BookmarkSubscribeRSS Feed
Ronein
Onyx | Level 15

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;
 
    
6 REPLIES 6
andreas_lds
Jade | Level 19

You may want to read the documentation of the set statement, especially the indsname-option seems to useful to solve the problem.

PaigeMiller
Diamond | Level 26

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. 

--
Paige Miller
Ronein
Onyx | Level 15
This is true but let's say that this structure is not defined by me and I see it as the status that I need to work with
Ronein
Onyx | Level 15

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 */

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 6 replies
  • 1412 views
  • 3 likes
  • 4 in conversation