BookmarkSubscribeRSS Feed
jnivi
Calcite | Level 5

Hi there, 

 

I have a fairly large dataset (data=have)with dates that spans from April 5, 2021 to December 31, 2022. 

I know the SAS date function usually assigns the week number based on the start of the year. However, I was wondering if there was another way to automatically assign a week number to each record in my dataset where 05Apr2021 to 11Apr2021 would be assigned week 1 (not week 0 ),  12Apr2021 to 17Apr2021 would be assigned week 2, and so forth. 

Obs       ID                  Date                     Week

1          1001          05APR2021                  1

2          1002         19 APR2021                  3

3          2040         01JAN2022                   

4         7680         08APR2021                   2

5         5432        30DEC2022

 

Thank you!!

 

 

2 REPLIES 2
Kurt_Bremser
Super User
proc sql noprint;
select min(date) into :begin
from have;
quit;

data want;
set have;
week = intck(`week`,&begin.,date,'c') + 1;
run;
AMSAS
SAS Super FREQ

Although the code @Kurt_Bremser has supplied works for the example data provided. You also need to be aware that it ONLY works assuming your input data earliest observation has date="05APR2021"d 

If you were to run this code against another dataset that which has a different value earliest value for date, e.g. 01APR2021 then week 1 is going to be 01APR2021- 07APR2021.

If you want have consistent behavior where week 1 is always 05APR2021-11APR2021 then you are going to want to remove the PROC SQL step and hard code your starting date (&begin) 

INTCK Function

data have ;
	format date date7. ;
	do i=3 to 100 by 3 ;
		date="04APR2021"d+i  ;
		output have ;
	end ;
run ;

proc sort data=have ;
	by date ;
run ;

data want;
	set have;
	week=intck("week","05APR2021"d,date,'c') + 1;
run;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

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
  • 882 views
  • 0 likes
  • 3 in conversation