I have two years' worth of daily data. I want to break it up into weekly data Monday to Sunday. I was going to write 104 if/then/else statements like the following, but I decided to ask if there is a better way. I am using enterprise guide 7.1 and sas 9.4. I am just comparing various metrics from week to week.
else if 'Pres Received Date'n gt '30dec2018'd and 'Pres Received Date'n le '06jan2019'd then seg = 1;
else if 'Pres Received Date'n gt '06jan2019'd and 'Pres Received Date'n le '13jan2019'd then seg = 2;
Definitely do not write 104 if-then statements 🙂
Is it important that seg is 1,2,3 and so on? If not, check out the WeekU. Format that will bin the variable for you
Definitely do not write 104 if-then statements 🙂
Is it important that seg is 1,2,3 and so on? If not, check out the WeekU. Format that will bin the variable for you
Use the WEEK() function? There are several variations that can be customzied for what you need.
Depending on how you're summarizing, you may be able to apply a format instead of creating a new variable as well.
https://documentation.sas.com/?docsetId=lefunctionsref&docsetTarget=n1ka2ulrvrjlasn0z7beco2yrgas.htm...
This is an example of using a formatted date in a summary table. Although dates are not annual, the summary is annual.
proc means data=sashelp.stocks N NMISS min max mean median STACKODS;
class date;
format date year4.;
var open high low;
output out=want n= mean= sum= median= mean= / autoname autolabel;
ods output summary = want2;
run;
@pangea17 wrote:
I have two years' worth of daily data. I want to break it up into weekly data Monday to Sunday. I was going to write 104 if/then/else statements like the following, but I decided to ask if there is a better way. I am using enterprise guide 7.1 and sas 9.4. I am just comparing various metrics from week to week.
else if 'Pres Received Date'n gt '30dec2018'd and 'Pres Received Date'n le '06jan2019'd then seg = 1;
else if 'Pres Received Date'n gt '06jan2019'd and 'Pres Received Date'n le '13jan2019'd then seg = 2;
@pangea17 wrote:
I have two years' worth of daily data. I want to break it up into weekly data Monday to Sunday. I was going to write 104 if/then/else statements like the following, but I decided to ask if there is a better way. I am using enterprise guide 7.1 and sas 9.4. I am just comparing various metrics from week to week.
else if 'Pres Received Date'n gt '30dec2018'd and 'Pres Received Date'n le '06jan2019'd then seg = 1;
else if 'Pres Received Date'n gt '06jan2019'd and 'Pres Received Date'n le '13jan2019'd then seg = 2;
Unless you have some shop requirement I would modify anything that requires name literals like: 'Pres Received Date'n
It might help to provide a small sample of your data and what the desired result would be, and if the result is a report (people read) or a data set needed by another process.
Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.
A format is very likely the way to go. I don't like the default behaviors of the SAS week formats:
data example; do d='25DEC2018'd to '18FEB2020'd; output; end; run; proc format library=work; /* %0W and %0U use different defintions for start of week and when to count a complete week the %0V */ picture myweek (default=8) low-high = '%Y-%0V' (datatype=date) ; run; proc freq data=example; format d myweek.; run;
Use V, U or W , check the documentation on either the Picture statement in Proc Format or the WEEK function with the U, V and W descriptor.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.