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

Dear experts,

 

I am trying to filter observation for latest 3 months with the condition in variable "Report_Month", I did manage to filter for currently month (October) with the code:

 

Where REPORT_MONTH = PUT(DATE(),MMYYS10.);

Would you mind instructing us, how can I filter the observations from a period (let say AUG/2018 to OCT/2018) ?

 

Notes: the value format of the variable looks like "08/2018", "09/2018","10/2018", etc.

 

Thank you for your helping.

It is never late for learning.
1 ACCEPTED SOLUTION

Accepted Solutions
gamotte
Rhodochrosite | Level 12

Hello,

 

If you want to manipulate date, you should first convert your string dates into date format.

 

data have;
input strdate $;
format date mmddyy10.;
date=input(strdate,anydtdte.);
put date;
cards;
01/2018
02/2018
03/2018
04/2018
05/2018
06/2018
07/2018
08/2018
09/2018
10/2018
;
run;

proc sql;
SELECT * FROM have
WHERE date BETWEEN '01Aug2018'd AND '01Sep2018'd;
quit;

View solution in original post

3 REPLIES 3
kiranv_
Rhodochrosite | Level 12

i will use month and intnx functions to solve this. something like below

 

data have;
date = '09Oct2018'D;
output;
date = '09Oct2018'D;
output;
date = '09SEP2018'D;
output;
date = '09AUG2018'D;
output;
date = '05AUG2018'D;
output;
date = '09JUN2018'D;
output;
format date date9.;;
run;

data want;
set have;
where month(date) le month(today())
and month(date) ge month(intnx('month', today(), -2)) ;
run;

 

gamotte
Rhodochrosite | Level 12

Hello,

 

If you want to manipulate date, you should first convert your string dates into date format.

 

data have;
input strdate $;
format date mmddyy10.;
date=input(strdate,anydtdte.);
put date;
cards;
01/2018
02/2018
03/2018
04/2018
05/2018
06/2018
07/2018
08/2018
09/2018
10/2018
;
run;

proc sql;
SELECT * FROM have
WHERE date BETWEEN '01Aug2018'd AND '01Sep2018'd;
quit;
Jagadishkatam
Amethyst | Level 16

Alternatively,

 

 

data want;
set have;
where input(substr(put(date,date9.),3),monyy7.) <= input(substr(put(today(),date9.),3),monyy7.) <= input(substr(put(date,date9.),3),monyy7.) ;
run;

 

Thanks,
Jag

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 3 replies
  • 3345 views
  • 2 likes
  • 4 in conversation