BookmarkSubscribeRSS Feed
catkat96
Obsidian | Level 7

I need to add a filter that would grab only data from the previous month. 

My code is the following: 

proc sql;
create table WANT as
select distinct id as id 
, max(date) format=ddmmyy10.
from have
where date between "01Apr2021"d and "31Apr2021"d
group by id;
quit;

As you can see it's pretty simple, but I would like to not have the dates hardcoded so it can be run automatically. 

I would like to grab all distinct ID from HAVE from the previous month

3 REPLIES 3
Kurt_Bremser
Super User

Do you want the previous month counted from today, or from the max date in the dataset?

Anyway, see here:

proc sql;
create table WANT as
  select distinct
    id as id 
    , max(date) format=ddmmyy10.
  from have
  where date between intnx('month',today,-1,'b') and intnx('month',today(),-1,'e')
  group by id
;
quit;
PGStats
Opal | Level 21

To extract all the data from the previous month into table WANT:

 

proc sql;
create table WANT as
select * 
from have
where date between intnx("month", today(), -1, "beginning") and intnx("month", today(), -1, "end");
quit;
PG
PGStats
Opal | Level 21

Note, if you want to process the extracted data further, you might be better to create a view instead of a table ( create view WANT as ... ) and then access that view in a subsequent query (create table meanProfits as select id, mean(profit) as meanProfit from WANT group by id; )

 

 

PG

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
  • 3 replies
  • 1661 views
  • 0 likes
  • 3 in conversation