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

Hi, 

I'm new to SAS. Can you help me with a more efficient code rather than the one I used below?

Dates for weekstart are increments of 7 days starting 01Nov2018.

Also, I do not want the code to have an end or 'until' date. 

 

 

data frequencydate; 
set frequency;
format weekstart date9.;
if week=0 then weekstart='01Nov2018'd;
else if week= 1 then weekstart='08Nov2018'd;
else if week= 2 then weekstart='15Nov2018'd;
else if week= 3 then weekstart='22Nov2018'd;
else if week= 4 then weekstart='29Nov2018'd;
else if week= 5 then weekstart='06Dec2018'd;
else if week= 6 then weekstart='13Dec2018'd;
else if week= 7 then weekstart='20Dec2018'd;
else if week= 8 then weekstart='27Dec2018'd;
else if week= 9 then weekstart='03Jan2019'd;
else if week= 10 then weekstart='10Jan2019'd;
else if week= 11 then weekstart='17Jan2019'd;
else if week= 12 then weekstart='24Jan2019'd;
else if week= 13 then weekstart='31Jan2019'd;
else if week= 14 then weekstart='07Feb2019'd;
else if week= 15 then weekstart='14Feb2019'd;
else if week= 16 then weekstart='21Feb2019'd;
else if week= 17 then weekstart='28Feb2019'd;
else if week= 18 then weekstart='07Mar2019'd;
else if week= 19 then weekstart='14Mar2019'd;
else if week= 20 then weekstart='21Mar2019'd;
else if week= 21 then weekstart='28Mar2019'd;
run;

 

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Here's a quick way. SAS dates are stored in number of days, so you can use basic arithmetic here. 

 

data frequencydate; 
set frequency;
format weekStart date9.;

start_date = '01Nov2018'd;
weekStart = start_date + week*7;

drop start_date;

run;

@Maiio wrote:

Hi, 

I'm new to SAS. Can you help me with a more efficient code rather than the one I used below?

Dates for weekstart are increments of 7 days starting 01Nov2018.

Also, I do not want the code to have an end or 'until' date. 

 

 

data frequencydate; 
set frequency;
format weekstart date9.;
if week=0 then weekstart='01Nov2018'd;
else if week= 1 then weekstart='08Nov2018'd;
else if week= 2 then weekstart='15Nov2018'd;
else if week= 3 then weekstart='22Nov2018'd;
else if week= 4 then weekstart='29Nov2018'd;
else if week= 5 then weekstart='06Dec2018'd;
else if week= 6 then weekstart='13Dec2018'd;
else if week= 7 then weekstart='20Dec2018'd;
else if week= 8 then weekstart='27Dec2018'd;
else if week= 9 then weekstart='03Jan2019'd;
else if week= 10 then weekstart='10Jan2019'd;
else if week= 11 then weekstart='17Jan2019'd;
else if week= 12 then weekstart='24Jan2019'd;
else if week= 13 then weekstart='31Jan2019'd;
else if week= 14 then weekstart='07Feb2019'd;
else if week= 15 then weekstart='14Feb2019'd;
else if week= 16 then weekstart='21Feb2019'd;
else if week= 17 then weekstart='28Feb2019'd;
else if week= 18 then weekstart='07Mar2019'd;
else if week= 19 then weekstart='14Mar2019'd;
else if week= 20 then weekstart='21Mar2019'd;
else if week= 21 then weekstart='28Mar2019'd;
run;

 

Thanks!


 

View solution in original post

3 REPLIES 3
Reeza
Super User

Here's a quick way. SAS dates are stored in number of days, so you can use basic arithmetic here. 

 

data frequencydate; 
set frequency;
format weekStart date9.;

start_date = '01Nov2018'd;
weekStart = start_date + week*7;

drop start_date;

run;

@Maiio wrote:

Hi, 

I'm new to SAS. Can you help me with a more efficient code rather than the one I used below?

Dates for weekstart are increments of 7 days starting 01Nov2018.

Also, I do not want the code to have an end or 'until' date. 

 

 

data frequencydate; 
set frequency;
format weekstart date9.;
if week=0 then weekstart='01Nov2018'd;
else if week= 1 then weekstart='08Nov2018'd;
else if week= 2 then weekstart='15Nov2018'd;
else if week= 3 then weekstart='22Nov2018'd;
else if week= 4 then weekstart='29Nov2018'd;
else if week= 5 then weekstart='06Dec2018'd;
else if week= 6 then weekstart='13Dec2018'd;
else if week= 7 then weekstart='20Dec2018'd;
else if week= 8 then weekstart='27Dec2018'd;
else if week= 9 then weekstart='03Jan2019'd;
else if week= 10 then weekstart='10Jan2019'd;
else if week= 11 then weekstart='17Jan2019'd;
else if week= 12 then weekstart='24Jan2019'd;
else if week= 13 then weekstart='31Jan2019'd;
else if week= 14 then weekstart='07Feb2019'd;
else if week= 15 then weekstart='14Feb2019'd;
else if week= 16 then weekstart='21Feb2019'd;
else if week= 17 then weekstart='28Feb2019'd;
else if week= 18 then weekstart='07Mar2019'd;
else if week= 19 then weekstart='14Mar2019'd;
else if week= 20 then weekstart='21Mar2019'd;
else if week= 21 then weekstart='28Mar2019'd;
run;

 

Thanks!


 

VDD
Ammonite | Level 13 VDD
Ammonite | Level 13
data frequencydate; 
length myi 8.;
format weekStart date9.;
	start_date = '01Nov2018'd;
	do i = 0 to 52;
		myi=i;
		weekStart = start_date + myi*7;
		output;
	end;
drop start_date myi;

run;
PGStats
Opal | Level 21

SAS provides date interval functions for handling date operations without having to refer to date values internal representation. For shifting dates, the function is INTNX :

 

data frequencydate; 
format weekStart yymmdd10.;
start_date = '01Nov2018'd;
do i = 0 to 21;
	weekStart = intnx("week", start_date, i, "sameday");
	output;
end;
drop start_date;
run;
PG

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 25. 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
  • 3 replies
  • 1013 views
  • 6 likes
  • 4 in conversation