BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ronein
Meteorite | Level 14

Hello

I want to ask a question please.

I have the following situation:

User define a month in macro parameter (for example 1706 which means June 2017)

%let last=1706;

 

I want to create a new parameter called vector that will take next 5 months (Each month is end of quarter)

so in this example vector parameter will get values  1709+1712+1803+1806+1809 (with + between the values)

I can write  %let vector=1709+1712+1803+1806+1809; 

But I want to ask how can it be created automatically ? 

How can I do it please?

 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Ok. Do like this then.

 

%let last=1706;

data _null_;
   last=input("&last.",yymmn4.);
   array dates{5} $ date1-date5;
   do i=1 to dim(dates);
      dates[i]=trim(put(intnx('month',last,i*3), yymmn4.));
   end;
   call symputx('fivedates', catx('+', of date1-date5));
run;

%put &fivedates.;

View solution in original post

8 REPLIES 8
Ronein
Meteorite | Level 14

I succeed to created the 5 dates 

Now I need to crerate a sas parameter that contain these values with + between them

How can I do it please?

 

 

Data a;
last_help=input("&last.",yymmn4.);
format last_help yymmn4.;
/*SAS date of &last. with format YYMM*/
date1 = intnx('month',last_help,3);
format date1 yymmn4.;
date_char1=put(date1,yymmn4.);


date2 = intnx('month',last_help,6);
format date2 yymmn4.;
date_char2=put(date2,yymmn4.);


date3 = intnx('month',last_help,9);
format date3 yymmn4.;
date_char3=put(date3,yymmn4.);

date4 = intnx('month',last_help,12);
format date4 yymmn4.;
date_char4=put(date4,yymmn4.);

date5 = intnx('month',last_help,15);
format date5 yymmn4.;
date_char5=put(date5,yymmn4.);


call symput("date_char1",trim(left(date_char1)));
call symput("date_char2",trim(left(date_char2)));
call symput("date_char3",trim(left(date_char3)));
call symput("date_char4",trim(left(date_char4)));
call symput("date_char5",trim(left(date_char5)));
Run;
%let vector=&date_char1.+date_char2.+date_char3.+date_char4.+date_char5.;
%put &vector.;

Kurt_Bremser
Super User

Now put the repeating code into a do loop.

Like

%let start=1706;
%let end=1809;

data _null_;
date = input("20&start.01",yymmdd8.);
length result $100;
do while (date < input("20&end.01",yymmdd8.));
  date = intnx('quarter',date,1,'s');
  result = catx('+',result,substr(put(date,yymmddn8.),3,4));
end;
call symputx('want',result);
run;

%put &want.;
Ronein
Meteorite | Level 14

I also found solution

Data a;
 last_help=input("&last.",yymmn4.);           
format last_help yymmn4.;
/*SAS date of &last. with format YYMM*/
date1 = intnx('month',last_help,3); 
format date1 yymmn4.;  
date_char1=put(date1,yymmn4.);


date2 = intnx('month',last_help,6); 
format date2 yymmn4.;  
date_char2=put(date2,yymmn4.);


date3 = intnx('month',last_help,9); 
format date3 yymmn4.; 
date_char3=put(date3,yymmn4.);

date4 = intnx('month',last_help,12); 
format date4 yymmn4.; 
date_char4=put(date4,yymmn4.);

date5 = intnx('month',last_help,15); 
format date5 yymmn4.;
date_char5=put(date5,yymmn4.);

call symput("date_char1",trim(left(date_char1)));
call symput("date_char2",trim(left(date_char2)));
call symput("date_char3",trim(left(date_char3)));
call symput("date_char4",trim(left(date_char4)));
call symput("date_char5",trim(left(date_char5)));

vector=CATX("+",&date_char1.,&date_char2.,&date_char3.,&date_char4.,&date_char5.); 
call symput("vectorx",trim(left(vector)));

Run;
%put &vectorx.;
Kurt_Bremser
Super User

@Ronein wrote:

I also found solution

Data a;
 last_help=input("&last.",yymmn4.);           
format last_help yymmn4.;
/*SAS date of &last. with format YYMM*/
date1 = intnx('month',last_help,3); 
format date1 yymmn4.;  
date_char1=put(date1,yymmn4.);


date2 = intnx('month',last_help,6); 
format date2 yymmn4.;  
date_char2=put(date2,yymmn4.);


date3 = intnx('month',last_help,9); 
format date3 yymmn4.; 
date_char3=put(date3,yymmn4.);

date4 = intnx('month',last_help,12); 
format date4 yymmn4.; 
date_char4=put(date4,yymmn4.);

date5 = intnx('month',last_help,15); 
format date5 yymmn4.;
date_char5=put(date5,yymmn4.);

call symput("date_char1",trim(left(date_char1)));
call symput("date_char2",trim(left(date_char2)));
call symput("date_char3",trim(left(date_char3)));
call symput("date_char4",trim(left(date_char4)));
call symput("date_char5",trim(left(date_char5)));

vector=CATX("+",&date_char1.,&date_char2.,&date_char3.,&date_char4.,&date_char5.); 
call symput("vectorx",trim(left(vector)));

Run;
%put &vectorx.;

I'd be hard pressed to imagine a more unwieldy "solution". See that @PeterClemmensen's solution and mine can be simply expanded by changing ONE parameter. Yours would need additional complicated code for every additional period, time and again.

Always strive for simplicity.

PeterClemmensen
Tourmaline | Level 20

First off, why do you want to do this? I can't think of any application where it will be of benefit? 

Ronein
Meteorite | Level 14

Why?

Because in my work office(insurance company) there is a program where there are 2 user defined parameters:

First parameter is specific month

Second parameter is 5 months after this month (in end of quarter)

My task is to make it more useful that user will define only one parameter  and the second parameter will be calculated automatically

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

So rather than just programmatically implementing a solution whereby the user puts in one single parameter - ie the 1706 - and the code does the rest, your going to write code which takes that parameter, and then creates more parameters, to pass in to further code. 

Well rather you than me, stack of cards and all that,  Me, I would have one parameter, and then in the code, use a range to get this information - i.e. intnx() that one paramter to find anything within a 5 month window. I mean you already have written the base SAS code, just implement that in the receiving code.

PeterClemmensen
Tourmaline | Level 20

Ok. Do like this then.

 

%let last=1706;

data _null_;
   last=input("&last.",yymmn4.);
   array dates{5} $ date1-date5;
   do i=1 to dim(dates);
      dates[i]=trim(put(intnx('month',last,i*3), yymmn4.));
   end;
   call symputx('fivedates', catx('+', of date1-date5));
run;

%put &fivedates.;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 990 views
  • 2 likes
  • 4 in conversation