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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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