Get Year and Month together as Value not as format

Accepted Solution Solved
Reply
Contributor
Posts: 36
Accepted Solution

Get Year and Month together as Value not as format


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 ?


Accepted Solutions
Solution
‎02-26-2015 06:11 PM
Esteemed Advisor
Posts: 7,285

Re: Get Year and Month together as Value not as format

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


All Replies
Esteemed Advisor
Posts: 7,285

Re: Get Year and Month together as Value not as format

What do your ordermth values look like?

Contributor
Posts: 36

Re: Get Year and Month together as Value not as format

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..

Solution
‎02-26-2015 06:11 PM
Esteemed Advisor
Posts: 7,285

Re: Get Year and Month together as Value not as format

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;

Contributor
Posts: 36

Re: Get Year and Month together as Value not as format

Thanks!!

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

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