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

Hello!

I'm working with a dataset and I am trying to compile a frequency output of some sort for a range of dates. To expand, I have a list of dates and I want to run code that looks at the list of dates and outputs how often dates fall within a weekly timeframe. 

 

10DEC2019

11DEC2019

12DEC2019

13DEC2019

14DEC2019

....

This goes all the way until May, with not all dates between December and May included

data want;
	set have;
	WHERE date1 between '10DEC2019'd and '17DEC2019'd;
run;

Currently this is what I am using to find frequency of dates in week intervals, by counting the results of the new dataset. However as you can imagine this is a slow process. I was wondering if there is a more automated and more effective way of doing this. Thank you!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Just apply a week format to the data.

 

proc freq data=have noprint;
table date1 / out=want;
format date1 weeku3.;
run;

https://gist.github.com/statgeek/0cae5568752959b035516d6ac07a20fb

 


@mitrakos wrote:

Hello!

I'm working with a dataset and I am trying to compile a frequency output of some sort for a range of dates. To expand, I have a list of dates and I want to run code that looks at the list of dates and outputs how often dates fall within a weekly timeframe. 

 

10DEC2019

11DEC2019

12DEC2019

13DEC2019

14DEC2019

....

This goes all the way until May, with not all dates between December and May included

data want;
	set have;
	WHERE date1 between '10DEC2019'd and '17DEC2019'd;
run;

Currently this is what I am using to find frequency of dates in week intervals, by counting the results of the new dataset. However as you can imagine this is a slow process. I was wondering if there is a more automated and more effective way of doing this. Thank you!

 


 

View solution in original post

3 REPLIES 3
ballardw
Super User

@mitrakos wrote:

Hello!

I'm working with a dataset and I am trying to compile a frequency output of some sort for a range of dates. To expand, I have a list of dates and I want to run code that looks at the list of dates and outputs how often dates fall within a weekly timeframe. 

 

10DEC2019

11DEC2019

12DEC2019

13DEC2019

14DEC2019

....

This goes all the way until May, with not all dates between December and May included

data want;
	set have;
	WHERE date1 between '10DEC2019'd and '17DEC2019'd;
run;

Currently this is what I am using to find frequency of dates in week intervals, by counting the results of the new dataset. However as you can imagine this is a slow process. I was wondering if there is a more automated and more effective way of doing this. Thank you!

 


You need to explain more about what you mean by " with not all dates between December and May included". Does that mean you have data in the interval that you need to exclude? Then you need to provide the rules of which to exclude.

Or does it mean that you just don't happen to have any data on certain dates? Likely nothing needs to be done.

 

You might also need to describe what you are defining as "week". SAS has three different standard definitions of "week" depending on how you define the first day of the week and involving year-end-boundaries.

 

Here is some example code showing dates and the week format (or similar to the WEEK functions).

data _null_;
   file print ;
   do date='25DEC2019'd to '07JAN2021'd;
   put date=date9. " WeekU: " date weeku. " WeekV: " date weekv. " WeekW: " date weekw.;
   end;
run;

If you use a 5 or 6 wide value, such as Weeku7 the result will have a result like:  20W05 to indicate the 5th week of 2020.

You can also make custom formats that might work. Why the discussion of formats? You can create groups that would be recognized by procs like Freq to create a group, or for graphing.

 
Reeza
Super User

Just apply a week format to the data.

 

proc freq data=have noprint;
table date1 / out=want;
format date1 weeku3.;
run;

https://gist.github.com/statgeek/0cae5568752959b035516d6ac07a20fb

 


@mitrakos wrote:

Hello!

I'm working with a dataset and I am trying to compile a frequency output of some sort for a range of dates. To expand, I have a list of dates and I want to run code that looks at the list of dates and outputs how often dates fall within a weekly timeframe. 

 

10DEC2019

11DEC2019

12DEC2019

13DEC2019

14DEC2019

....

This goes all the way until May, with not all dates between December and May included

data want;
	set have;
	WHERE date1 between '10DEC2019'd and '17DEC2019'd;
run;

Currently this is what I am using to find frequency of dates in week intervals, by counting the results of the new dataset. However as you can imagine this is a slow process. I was wondering if there is a more automated and more effective way of doing this. Thank you!

 


 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 1768 views
  • 2 likes
  • 4 in conversation