Hello
Let's say that I have a macro with one parameter(date ) .
%macro RRR(Par);
....
.....
%MEND RRR;
Let's say that there is a macro variable that contain list of dates with + sign between them.
for example :
%let ListValues=20191201+2191202+20191213+20191217+20191228;
I can run the macro manually for each value in &ListValues :
RRR(20191201);
RRR(2191202);
RRR(20191213);
RRR(20191217);
RRR(20191228);
My question is how can I run the macro automatically for each argument in &ListValues ?
Please not that number of arguments in &ListValues might be different each time that I want to run it
Hi @Ronein While your questions are nice and I love your learning spirit, I am afraid the learning structure doesn't seem to be well designed or directed. The idea is to basically get familiar with why macro, when macro and then the how. This essentially means the execution component or in other words the SAS language familiarity is needed first and foremost, which to my mind seems to be missing. Once you get the hang of it, the need for parameter to substitute for constant values may arise and that's when the macro requirement surfaces.
I really hope your organization cares about development of skills of you folks in a nice linear way so that at some point you really feel that have actually learned and accomplished. To be honest, I regret to admit at my organization where everything is a macro right from simple assignments and beyond and they call this a coding style and the bureaucracy rejects any suggestions that I could offer. Jeez have mercy!
I know you are smart, and I trust with the right guidance you will be able to accomplish things easily.
Anyways for your need:
%macro list;
%let ListValues=20191201+2191202+20191213+20191217+20191228;
%do i=1 %to %sysfunc(countw(&listvalues,+));
%let need= %scan(&ListValues,&i,+);
%RRR(&need)
%end;
%mend list;
%t
Thank you.
Is it correct answer?
Should it be under data _null_? or can I run it as I wrote in following code?
%local i YYYYMMDD;
%do i=1 %to %sysfunc(countw(&ListValues));
%let YYYYMMDD= %scan(&ListValues, &i);
%RRR( &YYYYMMDD.);
%end;
May I ask if this way will also work well?
Create a data set with dates values that will be arguments of macro.
Data HelpTbl;
input ddate;
cards;
20191201
2191202
20191213
20191217
20191228
;
Run;
%macro RunMacros;
%do i=1 %to &nobs;
Data _null_;
Set HelpTbl(firstobs=&i obs=&i);
call symputx('YYYYMMDD',ddate);
run;
%RRR(Par=&YYYYMMDD);
%end ;
%mend RunMacros;
%RunMacros
@Ronein wrote:
May I ask if this way will also work well?
Create a data set with dates values that will be arguments of macro.
Data HelpTbl;
input ddate;
cards;
20191201
2191202
20191213
20191217
20191228
;
Run;
%macro RunMacros; %do i=1 %to &nobs; Data _null_; Set HelpTbl(firstobs=&i obs=&i); call symputx('YYYYMMDD',ddate);
run; %RRR(Par=&YYYYMMDD); %end ; %mend RunMacros; %RunMacros
That could work, depending on whether you have created the NOBS macro variable with the number of observations in the HELPTBL dataset before calling the RUNMACROS macro. And also on what values the RRR macro needs for the PAR parameter.
Inside the loop you are running two steps. The data step to find a specific observation and stuff the value of the DDATE variable it into a macro variable. You are not formatting the value so it will be the raw number that you read into the numeric variable DDATA converted to a string of digits using the BEST32. format. You then use the value of that macro variable in your call to the RRR macro.
Hi @Ronein While your questions are nice and I love your learning spirit, I am afraid the learning structure doesn't seem to be well designed or directed. The idea is to basically get familiar with why macro, when macro and then the how. This essentially means the execution component or in other words the SAS language familiarity is needed first and foremost, which to my mind seems to be missing. Once you get the hang of it, the need for parameter to substitute for constant values may arise and that's when the macro requirement surfaces.
I really hope your organization cares about development of skills of you folks in a nice linear way so that at some point you really feel that have actually learned and accomplished. To be honest, I regret to admit at my organization where everything is a macro right from simple assignments and beyond and they call this a coding style and the bureaucracy rejects any suggestions that I could offer. Jeez have mercy!
I know you are smart, and I trust with the right guidance you will be able to accomplish things easily.
Anyways for your need:
%macro list;
%let ListValues=20191201+2191202+20191213+20191217+20191228;
%do i=1 %to %sysfunc(countw(&listvalues,+));
%let need= %scan(&ListValues,&i,+);
%RRR(&need)
%end;
%mend list;
%t
Thank you so much.
I think that I have learned very well when I need macro but I still have question how to do it...
I am really happy to learn from you and thank you again.
Happy New Year!
@novinosrin - At least you haven't lost your sense of humour! Yes, putting all SAS code inside macros and calling it a coding style. I've seen it all too. I'd call it a pain in the proverbial. It is like building a house and only using a chainsaw to do it. Imagine how that would turn out!
Thank you Sir @SASKiwi for chiming in. I can sense the "Déjà Vu" with the banter that Guru @hashman and I had over a personal text exchange the notion to accept what is not right as great or in other words unlearn the little I know to survive. lol
Of course, Life is unfair is obvious and known, but the rejection of asking to be reasonable seems unwelcome.
In general, I'm learning life lessons or some that call EQ(emotional intelligence/quotient skills) to agree and be compliant with murphy's law-"stuff happens".
Future advertisements for job skills would be like:- 1. Hard coders need only apply 2. Sphaghetti coders preferred 3. Master's degree in a quantitative STEM discipline required. (yeah right for the spaghetti crap)
PS Why am i still here, being relatively young and new to industry, got to make my resume look good, pay off the money i owe my mother who supported my college education. Perhaps I might have to think of using this time in finding a bird on a dating site and settle down.:
Without a sense of humor it would be easy to go cuckoo. Macro abuse is pretty ubiquitous but in certain industries it verges on insane. Tons of "code" concocted in this fashion I've had to protrude through often made me think that those writing it believe that if a piece of code is not encapsulated in a macro and then called as such it just won't run.
Kind regards
Paul D.
@hashman - Love the term macro abuse! Can't think of a better description.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.