I have a whole list of state abbreviations that are saved as libnames. I am trying to reference them in a simple macro to generate tables from data in each of these tables. All the files I in these libraries are of the form: state_commonname_date.
The code I had set up for this macro was:
%macro want (state = , date = );
data &state._history;
set &state..&state._commonname_&date.;
run;
%mend want;
For example the table I am setting for Alabama would be al.al_commonname_20180526. Looks like the issue is stemming from the parsing of the periods in the set command.
I suspect that your getting bit by the tokenizer macro interaction bug. Your code will run fine outside of a macro. But inside a macro the dataset name looks like multiple words to SAS instead of just one.
What I like to do is build the name into a macro variable and then use that.
%macro want (state = , date = );
%local indsn outdsn;
%let indsn=&state..&state._commonname_&date.;
%let outdsn= &state._history;
data &outdsn;
set &indsn;
run;
%mend want;
You can also just wrap it in an %unquote() so that it is seen as a single token.
I suspect that your getting bit by the tokenizer macro interaction bug. Your code will run fine outside of a macro. But inside a macro the dataset name looks like multiple words to SAS instead of just one.
What I like to do is build the name into a macro variable and then use that.
%macro want (state = , date = );
%local indsn outdsn;
%let indsn=&state..&state._commonname_&date.;
%let outdsn= &state._history;
data &outdsn;
set &indsn;
run;
%mend want;
You can also just wrap it in an %unquote() so that it is seen as a single token.
So this is perfect and I'll mark it as the correct solution. I just had a follow up question. If I have a list of around 8 states for a given date, how would I go about adapting this code so that I can put the 8 states into a list and have the macro loop through those 8 states for a fixed date so that 8 separate tables are created?
So I would want:
al.al_commonname_20180526
wv.wv_commonname_20180526
wa.wa_commonname_20180526, etc.
do you want _history created in WORK library or state(AL) library? Check for the extra period in data statement.
%macro want (state =al , date =20180526 );
data &state.._history;/* Looks like your missing a period here */
set &state..&state._commonname_&date.;
run;
%mend want;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.