BookmarkSubscribeRSS Feed
rohitguptaecb
Calcite | Level 5

I have a dataset like this.

Id    Sal    date

1     100    201108

2     200    201003

3     500    201304

4    300     200904

5    1000   200608

6    450     201201

Here, date is a variable that has numeric values. First four digits indicates year and last two digits indicates month. Now i want those observations whose date are between today and same day teo years ago.

for ex between 6Apr2011  and 6Apr2013  . 

How can we fetch such result .. Plz help

3 REPLIES 3
Patrick
Opal | Level 21

You're having only year and month as a start so best you can do is select on a monthly level.

First convert the number to a SAS date, eg.

date=input(put(date,6.),yymmn6.);

The SAS date you're getting this way is always on the first of the month.

Then use in your selection intnx() to get the range right, eg.

where today()>= date > intnx('year',today(),-1,'s')

PGStats
Opal | Level 21

where date between intnx('year', today(), -2, 's') and  today()

PG
Tom
Super User Tom
Super User

Not sure what SQL has to do with it.  You might also consider converting SAS dates into to your YYYYMM decimal coding values.  If the table is large this has to advantage of not having to make conversions on every value in the table in order to test it.

To get today's date you can use TODAY() function or &SYSDATE9 macro variable. (difference is that &SYSDATE9 is set when the SAS session starts).

In macro code you could do:

%let today = %sysfunc(today(),yymmn6.);

%let twoyearsago = %eval(&today - 200) ;

Then you can do something like:

   select * from have where date between &twoyearsago and &today

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
  • 3 replies
  • 681 views
  • 1 like
  • 4 in conversation