BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
harshpatel
Quartz | Level 8
Hi,

I want to filter data based on month and year selection

IF month is January and year is 2020 then data should filter as January2020 and December2019

But if month is not in January and year is 2020 then data should filter as February2020 and January2020 only two Months

I want this in SAS coding

Basically I want to compare two months only but for January scenario bid different as explained above
Kindly help me on this
Thanks,
Harsh
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

So your criteria is month specified and previous month. This works but it'll depend exactly on how you're trying to use the parameters. This is a fully worked example that you can run but you'll need to apply the idea to your own code.

 

*input parameters;
%let month = January;
%let year = 2004;


*create macro variables for next step;
data _null_;
*convert to SAS date;
month_select = input(catt('01', substr("&month", 1, 3), &year), date9.);
*previous month;
month_prev = intnx('month', month_select, -1, 'b');

*create macro variables;
call symputx('month_select', month_select);
call symputx('month_prev', month_prev);

run;

*check macro variables;
%put %sysfunc(putn(&month_select, date9)) ;
%put %sysfunc(putn(&month_prev, date9)) ;


*filter data;
data stocks;
set sashelp.stocks;
where intnx('month', date, 0, 'b') in (&month_select., &month_prev.);
run;

Note the usage of INTNX() to align the dates to the beginning of hte month for the comparison to work. This is important as 01Jan2019 is not the same as 03Jan2019 and SAS dates require a day component. Setting everything to the first of the month negates this issue.

 

 

Here's a great, but longer and in depth, reference for dates and times in SAS
https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/...

 

 


@harshpatel wrote:
Then Data should be filter as
April 2020 and March 2020

 

View solution in original post

6 REPLIES 6
Reeza
Super User

Is it only those two months or does it need to be dynamic for other situations? 
Will you ever need March, April? Is the issue here crossing the year boundary where you want the most recent 2 months or something like that?

 

Either way, you can increment dates easily using INTNX().

 

Here's a great, but longer and in depth, reference for dates and times in SAS
https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/...

 


@harshpatel wrote:
Hi,

I want to filter data based on month and year selection

IF month is January and year is 2020 then data should filter as January2020 and December2019

But if month is not in January and year is 2020 then data should filter as February2020 and January2020 only two Months

I want this in SAS coding

Basically I want to compare two months only but for January scenario bid different as explained above
Kindly help me on this
Thanks,
Harsh

 

harshpatel
Quartz | Level 8
Hi,
I will pass macro
% let month= January
% let year= 2020
If I will pass these macros then data should be filter as January 2020 and December 2019
But if I will change macro
Month= February
Year= 2020
Then data should be filter as February 2020 and January 2020
Reeza
Super User
What if I pass month=April and year=2020?
harshpatel
Quartz | Level 8
Then Data should be filter as
April 2020 and March 2020
Reeza
Super User

So your criteria is month specified and previous month. This works but it'll depend exactly on how you're trying to use the parameters. This is a fully worked example that you can run but you'll need to apply the idea to your own code.

 

*input parameters;
%let month = January;
%let year = 2004;


*create macro variables for next step;
data _null_;
*convert to SAS date;
month_select = input(catt('01', substr("&month", 1, 3), &year), date9.);
*previous month;
month_prev = intnx('month', month_select, -1, 'b');

*create macro variables;
call symputx('month_select', month_select);
call symputx('month_prev', month_prev);

run;

*check macro variables;
%put %sysfunc(putn(&month_select, date9)) ;
%put %sysfunc(putn(&month_prev, date9)) ;


*filter data;
data stocks;
set sashelp.stocks;
where intnx('month', date, 0, 'b') in (&month_select., &month_prev.);
run;

Note the usage of INTNX() to align the dates to the beginning of hte month for the comparison to work. This is important as 01Jan2019 is not the same as 03Jan2019 and SAS dates require a day component. Setting everything to the first of the month negates this issue.

 

 

Here's a great, but longer and in depth, reference for dates and times in SAS
https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/...

 

 


@harshpatel wrote:
Then Data should be filter as
April 2020 and March 2020

 

harshpatel
Quartz | Level 8
Thanks Reeza,
Its Working for me

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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