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.
You say you want the past weeks data. But really, you want data from the latest 7 dates in your data, correct?
Correct!
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;
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?.
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?
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 .
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.
@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 ) )
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.
@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.
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
Is this data representative? Do you have only 1 obs per date? Is the data sorted descending by date?
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?
So really, you want to get the last 7 obs of your data, correct?
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.