BookmarkSubscribeRSS Feed
JP1234
Calcite | Level 5

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

10 REPLIES 10
novinosrin
Tourmaline | Level 20

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;
JP1234
Calcite | Level 5

Thanks! Perfect!

novinosrin
Tourmaline | Level 20

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;
ballardw
Super User

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)

 

mkeintz
PROC Star

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,

  1. The INPUTN function reads a value as if into a numeric variable.
  2. The PUTN function write a number as text according to the specific format.
  3. The INTNX function advances the date value (in macrovar D) by 1 month.
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
JP1234
Calcite | Level 5

Thanks!

Ksharp
Super User
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;
PeterClemmensen
Tourmaline | Level 20

I'm interested. Why do you want a string like that?

JP1234
Calcite | Level 5

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

andreas_lds
Jade | Level 19

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

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

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 10 replies
  • 2035 views
  • 6 likes
  • 7 in conversation