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-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
  • 4 replies
  • 488 views
  • 0 likes
  • 2 in conversation