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

Hello!

I am currently trying to do a frequency count for a range of dates. I've done the frequency using the weeku6 format using this code: 

proc freq data=work.test noprint;
	tables date1 / out=work.test2;
	format date1 weeku6.;
run;

This has given me an output of this (this is the beginning of the output):

procfreq.PNG

What I would like to do is write some code that will fill in the missing weeks in-between the range of weeks I have (this specific dataset goes from 19W43 to 20W19, but I am working with different datasets that have different start and end dates) as well as give them a count of 0. Is there a way to do this? I hope I explained this clear enough. Thank you so much for reading!!

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Create some data for testing:

data test;
input date1 yymmdd10.;
format date1 yymmdd10.;
datalines;
2020-01-01
2020-01-03
2020-01-10
2020-01-12
2020-02-05
2020-02-07
;

Then, apply your code:

proc freq data=work.test noprint;
  tables date1 / out=work.test2;
  format date1 weeku6.;
run;

Then, use a "look-ahead" to fill the gaps:

data want;
merge
  test2
  test2 (
    firstobs=2
    keep=date1
    rename=(date1=_date1)
  )
;
output;
date1 = date1 + 7;
do while (week(date1) lt week(_date1));
  count = 0;
  percent = 0;
  output;
  date1 = date1 + 7;
end;
drop _date1;
run;

View solution in original post

2 REPLIES 2
Kurt_Bremser
Super User

Create some data for testing:

data test;
input date1 yymmdd10.;
format date1 yymmdd10.;
datalines;
2020-01-01
2020-01-03
2020-01-10
2020-01-12
2020-02-05
2020-02-07
;

Then, apply your code:

proc freq data=work.test noprint;
  tables date1 / out=work.test2;
  format date1 weeku6.;
run;

Then, use a "look-ahead" to fill the gaps:

data want;
merge
  test2
  test2 (
    firstobs=2
    keep=date1
    rename=(date1=_date1)
  )
;
output;
date1 = date1 + 7;
do while (week(date1) lt week(_date1));
  count = 0;
  percent = 0;
  output;
  date1 = date1 + 7;
end;
drop _date1;
run;
mitrakos
Obsidian | Level 7
This worked beautifully thank you so much!!

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 2 replies
  • 929 views
  • 0 likes
  • 2 in conversation