Hello
User define a macro variable that contain dates in form YYMM.
The number of arguments in dates macro variable can be different (depends on the specific program run)
For example:
%let dates=1903+1908+1912+2102+2105+2106;
I want to create a dynamic progrmam that creates the following macro variables automtically
m1=1903
m2=1908
m3=1912
m4=2102
m5=2105
m6=2106
What is the way to do it please?
This can get you started
https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/n1qvxz5u3uru7yn1nk7q64ohvwak.htm
@Ronein wrote:
Hello
User define a macro variable that contain dates in form YYMM.
The number of arguments in dates macro variable can be different (depends on the specific program run)
For example:
%let dates=1903+1908+1912+2102+2105+2106;
I want to create a dynamic progrmam that creates the following macro variables automtically
m1=1903
m2=1908
m3=1912
m4=2102
m5=2105
m6=2106
What is the way to do it please?
Why would want to make your program more complicated by creating so many macro variables?
Just use the one you already have.
%let dates=1903+1908+1912+2102+2105+2106;
%do index=1 %to %sysfunc(countw(&dates,+));
%let date=%scan(&dates,&index,+);
%let sasdate=%sysfunc(inputn(20&date,yymmn6.));
... do stuff with the string &DATE and the actual date &SASDATE ...
%end;
PS Why are you storing years using only 2 digits?
Thanks,
In my work people use to write in form YYMM and not YYYYMM . (I work in Cyprus).
I want to create macro vraibles m1,m2,m3.....
In your code I didn't see the index of the macro variables names.
Should I put it in macro and then in _null_ data set ?
%let dates=1903+1908+1912+2102+2105+2106;
%Macro RRR;
%do i=1 %to %sysfunc(countw(&dates,+));
%let m&i=%scan(&dates,&i,+);
%let sasdate&i.=%sysfunc(inputn(20&&m&i..,yymmn6.));
%end;
%mend;
DATA _null_;
%RRR;
Run;
What is it you are doing with them? Do you really need to reference all 6 at the same time? In general it probably will just make them harder to use to create so many macro variables.
Your code since it is wrapped in a macro is going to by default create LOCAL macro variables that will disappear when the macro ends. You could add a %GLOBAL statement.
%global m&i sasdate&i ;
%let m&i=%scan(&dates,&i,+);
%let sasdate&i.=%sysfunc(inputn(20&&m&i..,yymmn6.));
Wrapping a macro call that does not produce any SAS statements inside a DATA step is useless and just confusing to you and whoever needs to read your program.
If you do need to run a data step then you could also just use data step code to generate macro variables from your list. Data step code is much easier to debug.
data _null_;
do i=1 to countw("&dates",'+');
call symputx(cats('m',i),scan("&dates",i,'+'),'g');
call symputx(cats('sasdate',i),input(scan("&dates",i,'+'),yymmn4.),'g');
end;
run;
https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/p1fa0ay5pzr9yun1mvqxv8ipzd4d.htm
Hint: What value is the code using for the SYMBOL-TABLE optional argument?
Essentially. But the rules are a little more complex than that.
If the macro variable already exists then the visible one (the one at the lowest/closest symbol tables) is updated. That could be a GLOBAL macro variable or one local to the current macro that is running, but it might be one that is local to a macro that is running that called this macro.
If the macro variable does not exist then one is created in the lowest symbol table. Which might be the GLOBAL table, for example if you ran the data step outside of a macro.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.