BookmarkSubscribeRSS Feed
Vasundha
Calcite | Level 5

Hi All,

I need a small help from you all. I've a table with a date variable in that I wanna get past week's data. I used a condition called where=(date= today() -7) but the thing here is for that date variable it has no weekends and Mondays in it. Please help me out to get the past 7 days data irrespective of date values in it. Thanks in advance.

22 REPLIES 22
PeterClemmensen
Tourmaline | Level 20

You say you want the past weeks data. But really, you want data from the latest 7 dates in your data, correct?

Vasundha
Calcite | Level 5

Correct!

PeterClemmensen
Tourmaline | Level 20

Here's an approach. See if you can use that as a template.

 

proc rank data = sashelp.stocks out = rank ties = dense descending;
   var date;
   ranks r;
run;

data want;
   set rank;
   where r <= 7;
run;
Vasundha
Calcite | Level 5

Appreciate it. Can't it be possible to keep a condition in the input table in a data step?.

Data want;

Set have (condition);

Somewhat like that?.

 

PeterClemmensen
Tourmaline | Level 20

No. Not directly. You would have to do some pre-processing of your data. Otherwise, how would you know the 7'th largest date in your data?

Vasundha
Calcite | Level 5

7th largest?. What I meant was I require a condition to get the past 7 days data irrespective of dates in my table. And FYI, they don't have weekends and Mondays .

PeterClemmensen
Tourmaline | Level 20

Same thing. SAS Dates are integers. If you want to retrieve data for the latest 7 days of your data, you would have to know the 7'th largest value of date and retrieve everything above that. 

ballardw
Super User

@Vasundha wrote:

Hi All,

I need a small help from you all. I've a table with a date variable in that I wanna get past week's data. I used a condition called where=(date= today() -7) but the thing here is for that date variable it has no weekends and Mondays in it. Please help me out to get the past 7 days data irrespective of date values in it. Thanks in advance.


That code wouldn't work because you are using =, so it only finds one date.

Did you try

 where=( date ge ( today() -7 ) )
Vasundha
Calcite | Level 5

Thanks for replying me, I've tried your code it gives me just 3 dates cuz when we're using today () -7 function in that condition it is counting weekends, Mondays as well. That's why I got only 3 dates as output.

 

Explanation : today () -7 

17Nov22

16Nov22

15Nov22.

Alongside these 3 dates I must also get 11Nov22, 10Nov22, 09Nov22 & 08Nov22.

Note: like I mentioned earlier I've no weekends and Mondays in my data for some reason.

PeterClemmensen
Tourmaline | Level 20

@Vasundha , did you try the approach that I posted above? If it does not suit you and you have not yet found your answer, I encourage you to post some sample data for us to work with that resembles your actual data and your desired result. Makes it much easier to provide a usable code answer. 

Vasundha
Calcite | Level 5

Sure, here's my data.

Data have;
Input in_date : date9. readings;
format in_date date9.;
Datalines;
18NOV2022 10
17NOV2022 12
16NOV2022 15
15NOV2022 45
11NOV2022 67
10NOV2022 60
09NOV2022 61
08Nov2022 89
04Nov2022 40
;
run;
Note: I require a condition in the input table to get the  past 7 days data i.e

@PeterClemmensen wrote:

@Vasundha , did you try the approach that I posted above? If it does not suit you and you have not yet found your answer, I encourage you to post some sample data for us to work with that resembles your actual data and your desired result. Makes it much easier to provide a usable code answer. 


A condition like this : 
Data want;
Set have(condition);
run;
 
 
 
 
18Nov2022 10 17Nov2022 12 16Nov2022 15 15Nov2022 45 11Nov2022 67 10Nov2022 60 09Nov2022 61
PeterClemmensen
Tourmaline | Level 20

Is this data representative? Do you have only 1 obs per date? Is the data sorted descending by date?

Vasundha
Calcite | Level 5

Yes, I've only one obs per date and they're in ascending order in my table. My bad I gave you data in descending order. Please Nevermind.


@PeterClemmensen wrote:

Is this data representative? Do you have only 1 obs per date? Is the data sorted descending by date?


 

PeterClemmensen
Tourmaline | Level 20

So really, you want to get the last 7 obs of your data, correct?

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 22 replies
  • 1127 views
  • 0 likes
  • 4 in conversation