BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
pangea17
Quartz | Level 8

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;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

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

View solution in original post

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20

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

Reeza
Super User

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;

 

 


 

ballardw
Super User

@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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 1487 views
  • 3 likes
  • 4 in conversation