SAS Programming

DATA Step, Macro, Functions and more
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-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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