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
Amethyst | Level 16

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
Amethyst | Level 16

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
Amethyst | Level 16

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
Amethyst | Level 16

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!

yabwon
Amethyst | Level 16

To understand Kurt's code I would suggest to start with reading about call execute() routine:

1) a blog: https://blogs.sas.com/content/sgf/2017/08/02/call-execute-for-sas-data-driven-programming/

2) an article: https://support.sas.com/resources/papers/proceedings/proceedings/sugi22/CODERS/PAPER70.PDF (by guru Ian Whitlock)

3) the doc: https://documentation.sas.com/?docsetId=mcrolref&docsetTarget=n1q1527d51eivsn1ob5hnz0yd1hx.htm&docse...

 

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

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.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

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!

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
  • 8 replies
  • 1746 views
  • 1 like
  • 3 in conversation