BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
kajal_30
Quartz | Level 8

Hi Team,

 

I have a date as input and I need to get last 6 dates in a continues manner for the same gap between dates.

for eg I have a date %let yearmt = 202201 so I need dates as 202110 ,202107,202104,202101,202010,202007 . How can I calculate it using a macro? 

 

Regards

Kajal

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

Use built-in SAS functions to compute months (and all calendar arithmetic)

 

%let yearmt=202201;
%let yearmtn=%sysfunc(inputn(&yearmt,yymmn6.));

%macro dothis;
    %do i=1 %to 6;
        %let thismonth=%sysfunc(intnx(month,&yearmtn,%eval(&i*-3),b));
        %put %sysfunc(putn(&thismonth,yymmn6.));
    %end;
%mend;
%dothis
--
Paige Miller

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

Use built-in SAS functions to compute months (and all calendar arithmetic)

 

%let yearmt=202201;
%let yearmtn=%sysfunc(inputn(&yearmt,yymmn6.));

%macro dothis;
    %do i=1 %to 6;
        %let thismonth=%sysfunc(intnx(month,&yearmtn,%eval(&i*-3),b));
        %put %sysfunc(putn(&thismonth,yymmn6.));
    %end;
%mend;
%dothis
--
Paige Miller
ballardw
Super User

Why does the subject line for your post say "4 calendar dates" and the body of the question "last 6 dates"?

 

Given any "date" what is the rule for determining what the dates would be? I can guess but really would prefer not to rely on guesses when programming.

 

Do all of the values go into one macro variable? 4 macro variables? 6 macro variables? Data step variable(s)? In other words, what is the result to look like.

 

Note: Macro variables are text. So 202201 is not a "date" but 6 characters.

 

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 3 replies
  • 256 views
  • 2 likes
  • 3 in conversation