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 April and year is 2020 then data should filter as 2020 , 2019 and 2018

But if month is not in April and year is 2020 then data should filter as 2020 and 2019 only two years

I want this in SAS coding

 

For April it should come three years. This year, Previous Year and Previous to previous Year (3 years)

For Other Months it should come two years. This year, Previous Year  (2 years)

 

Kindly help on this

 

Regards,

Harsh

 

1 ACCEPTED SOLUTION

Accepted Solutions
sustagens
Pyrite | Level 9
/*Assuming your month and year selections are stored in macro variables:*/

%let month=4;
%let year=2020;

data _null_;
if &year eq 2020 then do;
if &month eq 4 then call symput('filter','(where=(2018 le year le 2020))');
else if &month ne 4 then call symput('filter','(where=(2019 le year le 2020))');
end;
run;

data have;
input Year Rate;
format Rate 8.2;
datalines;
2016 45.31
2017 44.72
2018 47.71
2019 49.26
2020 51.42
2021 52.65
;
run;

data want;
set have &filter.;
run;

View solution in original post

4 REPLIES 4
ketpt42
Quartz | Level 8

Here's some untested code for 1 of many possible solutions.

data _null_;
    end_year=year(today());
    current_month=month(today());
    if current_month=4 then start_year=end_year - 2;
    else start_year = end_year - 1;
    call symputx('end_year', end_year);
    call symputx('start_year', start_year);
run;

%put &=start_year &=end_year;

 Edit: My assumption was that you meant if current year was 2020.

Patrick
Opal | Level 21

The coding will depend on your data like if you've got SAS date or datetime values. Kindly post some sample data in the form of a tested SAS data step creating such data and then show us the desired outcome using this sample data.

sustagens
Pyrite | Level 9
/*Assuming your month and year selections are stored in macro variables:*/

%let month=4;
%let year=2020;

data _null_;
if &year eq 2020 then do;
if &month eq 4 then call symput('filter','(where=(2018 le year le 2020))');
else if &month ne 4 then call symput('filter','(where=(2019 le year le 2020))');
end;
run;

data have;
input Year Rate;
format Rate 8.2;
datalines;
2016 45.31
2017 44.72
2018 47.71
2019 49.26
2020 51.42
2021 52.65
;
run;

data want;
set have &filter.;
run;
harshpatel
Quartz | Level 8

Thank you so much, It works for me

 

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