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.

 

 

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
  • 3 replies
  • 753 views
  • 2 likes
  • 3 in conversation