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

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

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

 

View solution in original post

10 REPLIES 10
Ronein
Meteorite | Level 14

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;
Ronein
Meteorite | Level 14

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

 

Tom
Super User Tom
Super User

@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.

novinosrin
Tourmaline | Level 20

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

 

Ronein
Meteorite | Level 14

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!

 

SASKiwi
PROC Star

@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!

novinosrin
Tourmaline | Level 20

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.:Smiley Wink

 

 

hashman
Ammonite | Level 13

@SASKiwi:

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.  

SASKiwi
PROC Star

@hashman  - Love the term macro abuse! Can't think of a better description.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 10 replies
  • 902 views
  • 5 likes
  • 6 in conversation