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

I want to generate multiple datasets dynamically and automatically using macro variable reference.
The basic idea is to use the elements of below list as iterators:

 

libname lsa 'C:\Users\mayqy015\Desktop\SAS Holder\loan_size_analysis';

%let 
codelist
=
_beijing
_tianjin
...
_hongkong
_macao
;

These iterators will become the key part of dynamic referencing, and here below are the following codes:

 

%macro covid19trimming(code);

data lsa.&code._COVID19_treated; 
set lsa.&code; 
if project_opentime_cn<20200101 then delete; 
if project_opentime_cn>20200331 then delete; 
run;
%mend;

%macro try;
	%let word_cnt=%sysfunc(countw(&codelist));
	%do i = 1 %to &word_cnt;
		%let code=%qscan(%bquote(&codelist),&i);
		%covid19trimming(&code);
	%end;
%mend;

%try;

Unfortunately, even though the code can be run without mistakes, the SAS system cannot recognize my data step correctly. For example:

NOTE: There were 54752 observations read from the data set LSA._BEIJING.
NOTE: The data set LSA._BEIJING has 54752 observations and 80 variables.
NOTE: The data set WORK._COVID19_TREATED has 54752 observations and 80 variables.
NOTE: DATA statement used (Total process time):
      real time           2.40 seconds
      cpu time            0.57 seconds

My opinion is that the SAS system recognizes that there are two datasets to be created, LSA._BEIJING and _COVID19_TREATED.
But I actually want to create only ONE dataset named 'LSA._BEIJING_COVID19_TREATED'.
Can any kind and generous experts help me out?

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Onyx | Level 15

Maybe:

 

data lsa.%unquote(&code.)_COVID19_treated; 

will help?

 

Bart 

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

8 REPLIES 8
yabwon
Onyx | Level 15

Maybe:

 

data lsa.%unquote(&code.)_COVID19_treated; 

will help?

 

Bart 

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



yabwon
Onyx | Level 15

or maybe this:

%macro covid19trimming(code);
%let code = &code.; /* to remove spaces */
data lsa.&code._COVID19_treated;
_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



applemonster
Fluorite | Level 6

Yes! It works! But I still do not know why. I cannot quite understand SAS masking. Could you please explain a little bit? Or do you have some material that I can learn? Thank you so much for your generous help!

yabwon
Onyx | Level 15

Hi,

 

My favourite reading about macroquotting: https://stats.idre.ucla.edu/wp-content/uploads/2016/02/bt185.pdf

 

In your code you used %qscan() which not only scanned but also macroquotted scanned text and some "quitting leftovers" left attached to value of &code. 

 

If I may suggest something, the golden rule of macroquotting is:

"If you can do something with macroquoting or without macroquoting then do it without macroquoting",

so consider solution proposed by @Kurt_Bremser

 

All the best

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Kurt_Bremser
Super User

Store your codes in a dataset, and use that to call the macro repeatedly:

data control;
input code $20.;
datalines;
_beijing
_tianjin
_hongkong
_macao
;

data _null_;
set control;
call execute('%nrstr(%covid19trimming(' !! trim(code) !! '))');
run;
applemonster
Fluorite | Level 6

Hi, @Kurt_Bremser 

Your code is clean and tidy, but it is so far away from what I have learnt than I cannot understand. So could you please suggest some reference to videos and articles that I can learn your way?

 

Thank you in advance!

Kurt_Bremser
Super User

Apart from the documentation for CALL EXECUTE, which @yabwon already linked to, the use of %NRSTR is important. It prevents premature execution of macro code when the instructions are fed to the execution queue, and is usually a good idea when calling a macro with CALL EXECUTE.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 8 replies
  • 723 views
  • 1 like
  • 3 in conversation