Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Forcing variable into macro text string

Status
Not open for further replies.

jonnysnow

Programmer
Apr 10, 2003
36
US
How can I get a filename to contain the current month/year? It's easy to get numeric variables:

[tt]mth = month(today());
yr = year(today());[/tt]


But, later on I want a filename to actually contain these as strings, something like:

[tt]data file200310;[/tt]

I've tried:
[tt]
%let M = mth;
%let Y = yr;
data file&Y.&M;
[/tt]


but this just gives me

[tt]data fileyrmth;[/tt]

How do I get the value of these into text strings?
 
Johnnysnow,
It is a matter of timing, a macro term used for when the macro compiler resolves the macro var. You could use the '&&' in front of your macro var. This will resolve to the macro var at compile time. At run-time the value you want would be inserted. Of course this is too complicated for right now. It took me two years to understand how it works and I still don't understand all of it. Here is how I would do what you want to do. I gather that you want to name a file using a date of some type.
try this....
data _null_;
call symput('filename',trim(left(put(year(date()),8.)))||
trim(left(put(month(date()),8.))) );
run;
In the next step (data or proc) the macro var filename is available to your code. '&filename' will now be your date string. So..

ods pdf file="data &filename..pdf";
.....

I hope that this helps.
Klaz
 
How about using %sysfunc()..
so the code would be

%let mth= %sysfunc(today(),month);
%let yr = %sysfunc(today(),year);

data file&mth.&yr.;
set infile;
run;

hth
SK1
 
SK1,
I thought of that before I wrote the suggestion above. The problem I had with using the %sysfunc() macro function was would it work at all. I checked the SAS documentation for 8.2 and found that the TODAY() function was not listed in the list that the Sysfunc macro function supported. So I suggested the above example.
Klaz
 
The best way is to use a Data _null_ step as follows

data _null_;
date = put(today(),ddmmyy6.);
call symput('filedt',date);
run;

data file&filedt;
...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top