BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ani7
Obsidian | Level 7

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. 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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.

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

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.

Ani7
Obsidian | Level 7

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.

SuryaKiran
Meteorite | Level 14

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;
Thanks,
Suryakiran

sas-innovate-2024.png

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.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 2889 views
  • 0 likes
  • 3 in conversation