Hello
What is the way to write the following statements in array?
It should be a flexible program?
The user define dates that he is interested (vector macro parameter)
Then the program is working by these dates.
In the example show below the user defined 5 dates so should have 5 statements .
But in another case user might choose for example 3 dates and then need only 3 statements
IF S1 then YYMM_source=&m1;
IF S2 then YYMM_source=&m2;
IF S3 then YYMM_source=&m3;
IF S4 then YYMM_source=&m4;
IF S5 then YYMM_source=&m5;
Data ttt2009;
input id z w y;
cards;
1 9 0 10
2 9 0 20
3 8 0 30
;
run;
Data ttt2006;
input id z w y;
cards;
1 9 0 12
2 9 0 23
3 8 0 34
;
run;
Data ttt2003;
input id z w y;
cards;
1 9 0 54
2 9 0 23
3 8 0 42
;
run;
Data ttt1912;
input id z w y;
cards;
1 9 0 17
2 9 0 54
3 8 0 26
;
run;
Data ttt1909;
input id z w y;
cards;
1 9 0 34
2 9 0 16
3 8 0 43
;
run;
Data ttt1906;
input id z w y;
cards;
1 9 0 34
2 9 0 19
3 8 0 25
;
run;
Data ttt1903;
input id z w y;
cards;
1 9 0 34
2 9 0 25
3 8 0 18
;
run;
%let vector=2009+2006+2003+1912+1909;
%let n =%sysfunc(countw(&vector));
%put &n;
/*%put &m1;*/
/*%put &m2;*/
/*%put &m3;*/
/*%put &m4;*/
/*%put &m5;*/
%macro sset;
%do j=1 %to &n.;
ttt&&m&j.. (where=(Z=9 and W=0)in=S&j.)
%end;
%mend;
%put %sset;
Data tbl_all;
set %sset;
by ID;
/*Check how do it automatically???*/
IF S1 then YYMM_source=&m1;
IF S2 then YYMM_source=&m2;
IF S3 then YYMM_source=&m3;
IF S4 then YYMM_source=&m4;
IF S5 then YYMM_source=&m5;
Run;
You may want to read the documentation of the set statement, especially the indsname-option seems to useful to solve the problem.
This whole problem becomes incredibly easy if you don't put calendar year/month into data set names or variable names. Use one long data set, with a variable to indicate the year and month, and the problem is much much easier.
Use the INDSNAME= option of the SET statement instead of the multiple IN= dataset options.
It is perfect , I am still interested to know if it is possible to write the IF's statements using macro or array?
Data tbl_all;
set %sset indsname=ds;
by ID;
YYMM_source=substr(scan(ds,2,'.'),4);
Run;
/*WORK.TTT2009 */
What is contained in macro variables m1 to m5 in your original example?
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.