start='01feb2017'd;
end='05may2018'd;
I want to generate a string
IND_FEB17,IND_MAR17, IND_APR17, IND_MAY17,.....IND_ARP18, IND_MAY18
how to use a marcro to do that?
Thanks.
Jeff
Very simple with datastep syntax than macro syntax IMHO
data w;
do i=0 to intck('month','01feb2017'd,'05may2018'd);
list=catx('_','IND',put(intnx('month','01feb2017'd,i),monyy.));
output;
end;
drop i;
run;
proc sql;
select list into :list separated by ','
from w;
quit;
%put &=list;
Thanks! Perfect!
You can macrotise the above with a macro wrapper like
dm log 'clear';
%macro t(start='01feb2017'd,end='05may2018'd);
%global list;
data w;
do i=0 to intck('month',&start,&end);
list=catx('_','IND',put(intnx('month',&start,i),monyy.));
output;
end;
drop i;
run;
proc sql noprint;
select list into :list separated by ','
from w;
quit;
%mend t;
%t
%put list=&list;
It may help to tell us how you intend to use the resulting string.
If it is supposed to represent a list of data sets or variables you may be able to us list notation such as
IND_: all variables or datasets that start with ind_
Ind_feb17 - ind_may18 possibly, depends on actual values of other dataset names or variables as month names or abbreviations are a not well thought out value for sorting/selection in order
ind_feb17 -- ind_may18 (adjacent variables in a set)
I agree with @ballardw that you might get more benefit from the forum if you let us know why you need this list. There may be many shortcuts that don't require itemizing the elements of the list.
However, this can be done fairly directly in a macro:
%macro monyy_list(prefix=,start=,end=);
%global strng;
%do d=%sysfunc(inputn(&start,date9.)) %to %sysfunc(inputn(&end,date9.));
%let strng=&strng &prefix%sysfunc(putn(&d,monyy5.));
%let d=%sysfunc(intnx(month,&d,1));
%end;
%mend monyy_list;
%monyy_list(prefix=IND_,start=01feb2017,end=05may2018);
%put &=strng;
The %sysfunc macro function is a way to invoke regular SAS functions in the macro environment. In this case,
Thanks!
data x;
start='01feb2017'd;
end='05may2018'd;
length want $ 800;
do i=start to end ;
if month(i) ne month then do;
want=catx(',',want,cats('IND_',put(i,monyy5.)));
end;
month=month(i);
end;
put want=;
run;
I'm interested. Why do you want a string like that?
Thank you very much. It works!
The string contains field names form a dateset which is updated monthly with a new field name added to it.
Jeff
@JP1234 wrote:
Thank you very much. It works!
The string contains field names form a dateset which is updated monthly with a new field name added to it.
Jeff
Adding fields every month is only necessary if the data is stored in variable-names. That is bad design and fixing it almost always allows easier using of the data.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.