Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting Date Values in SAS

Status
Not open for further replies.

sasshelp

Programmer
Mar 16, 2007
1
0
0
US
I am trying to create a sas job that assigns a week number to a group of 7 dates. So for example, March 1 - March 7 is Week 1, March 8 through March 15, is Week 2...etc.

I'd originally thought an 2-dim array would resolve this problem, but have given up on that idea.

The other option was to designate a system date value as Start date and increment Start date by a certain number. If the variable ddate is between the min and max start dates then a week number would assigned. Example here:

&start=17112; /* Denotes sys date value of 11/7/06 */
&end = 17139; /* Denotes sys date value of 12/4/06 */

data test;
input @1 ddate PD5.0; /*variable ddate is yyddmm. format*/

week=0;
start=&start;

if start <= ddate <= (start +6) then week=1;
if (start+7) <= date <= (start+13) then week=2;
IF (START+14) <= DDATE<= (START+20) THEN WEEK=3;
IF (START+21) <= DDATE<= (START+27) THEN WEEK=4;
IF (START+28) <= DDATE<= (START+30) AND DDATE<=END THEN WEEK=5;
END;

format ddate yymmdd10.;
run;

The problem that occurs is that SAS does not seem to be able to recognize DDate as having a system date value that falls between start point 1 and start point 2. Therefore, when I run a proc freq; tables week;, I get no returned values. The data infile has ddates that are between the start and end dates.


Note: This program is set up in a mainframe environment (I am not using PC SAS).
 
Hi Sasshelp,

I did not get your question completely, but I will have a jab at it anyway.

If you want the week to be computed as the week of the year, try using the WEEK function in SAS.

The syntax would be: week(<sas_date>,<modifier>). You can look up more on this function using SAS help and determine the modifier that you need. This function returns the week number in a year.

If you want to determine the count of time intervals between two date intervals (which I think you want to do), use the INTCK function instead of adding 7 to the start date.

For example, intck('week','12MAR2007'd,'18MAR2007'd) would return 1 week.

Hope this helps!

Sarav
 
sashelp,
I don't understand why you do not use the following sas statement:

Code:
week = int((ddate - start) / 7)+1;

If you want to have all dates after the December 4, 2006 to be lumped together with week 5 values, use a reset statement. Like IF WEEK > 5 THEN WEEK = 5;

Hope that this helps you,
Klaz
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top