I'm trying to use a macro variable as part of a dataset name and not having any success. Here's a simplifed version of what I'm trying to do that generates a Syntax Error message . I want to get a dataset name of 'US000D_output'. I'm missing something simple but it is eluding me.
Thanks in advance for any help you can provide,
Gene
data input;
Length text $30 station $6;
Text='/Prefix/US000D/Suffix/';
Station=scan(text,2);
Call symput('StationID',Station);
put &StationID;
run;
data &StationID_output;
set input;
run;
You need a period to tell SAS where the macro variable name stops. Also a macro variable created with CALL SYMPUT will not resolve until a step boundary is reached.
data input;
Length text $30 station $6;
Text='/Prefix/US000D/Suffix/';
Station=scan(text,2);
Call symput('StationID',Station);
run;
%put &StationID;
data &StationID._output;
set input;
run;
You need a period to tell SAS where the macro variable name stops. Also a macro variable created with CALL SYMPUT will not resolve until a step boundary is reached.
data input;
Length text $30 station $6;
Text='/Prefix/US000D/Suffix/';
Station=scan(text,2);
Call symput('StationID',Station);
run;
%put &StationID;
data &StationID._output;
set input;
run;
You might also want to consider making sure that the resolved name doesn't exceed the SAS naming limits. Nothing like trying to use "too_long_of_a_macrovariable_output" only to later find the actual name of the data set is "too_long_of_a_macrovariable_out" because of the 32 character limit.
Similar considerations are in effect for Library, Catalog and File reference names.
You should only use the ancient CALL SYMPUT() routine when it is important that leading and/or trailing spaces be put into the macro variable.
Instead use its replacement the CALL SYMPUTX() routine.
If it worked for you it is only because the result of the SCAN() function call completely filled the variable STATION which you defined as length $6. If STATION had been defined longer or the value pulled out of TEXT had been shorter you would have put one or more trailing spaces into the macro variable STATIONID.
In which case your data step would have made two output datasets since you would have told SAS to run something like:
data U0000D _output ;
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.