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


I have to select values from a table based on months. I have to select last 3 months data. Have already tried the following -

data _null_;

set a(obs=1);

prev_2_mon_dt = intnx('month',date(),-2,'beginning');

prev_2_mon = year(prev_2_mon_dt)*100 + month(prev_2_mon_dt);

call symput('pr2mn',prev_2_mon);

run;

data c;

set d;

if ordermth in (%eval(&pr2mn.),%eval(&pr2mn. + 1),%eval(&pr2mn. + 2));

run;

So it gives numebrs like - 201411,201412, 201413 etc..

Is there any other way to handle this issue ?

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

Your formula would produce something like 20152 rather than 201502.

I'd use something like:

data _null_;

  prev_2_mon_dt = intnx('month',date(),-2,'beginning');

  call symput('pr2mn',put(prev_2_mon_dt,yymmn6.));

  call symput('curmn',put(date(),yymmn6.));

run;

data d;

  input ordermth;

  cards;

201410

201411

201412

201501

201502

201503

201504

201505

201506

  ;

 

data c;

set d;

if &pr2mn. le ordermth le &curmn.;

run;

View solution in original post

4 REPLIES 4
art297
Opal | Level 21

What do your ordermth values look like?

rajat051984
Fluorite | Level 6

They look like - 201410,201411, 201412,201501 etc..

They are calculated each time with the formula I used above. Year*100 + month.

It is calcualated from a timestamp but I don't have that timestamp information in the input file anymore..

art297
Opal | Level 21

Your formula would produce something like 20152 rather than 201502.

I'd use something like:

data _null_;

  prev_2_mon_dt = intnx('month',date(),-2,'beginning');

  call symput('pr2mn',put(prev_2_mon_dt,yymmn6.));

  call symput('curmn',put(date(),yymmn6.));

run;

data d;

  input ordermth;

  cards;

201410

201411

201412

201501

201502

201503

201504

201505

201506

  ;

 

data c;

set d;

if &pr2mn. le ordermth le &curmn.;

run;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 712 views
  • 0 likes
  • 2 in conversation