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

I would like to make 3 variables, start, stop and event.

yearIddeathydeathm
20011  
20021  
20031  
20041  
2005120051
20012  
20022  
20032  
2004220043
20013  
20023  
20033  
20043  
20053  

 

The table I'd like to see is below. Can I have it through both datastep and procsql? thanks in advance.

 

yearIddeathydeathmstartstopevent
20011  0520
20021  521040
20031  1041560
20041  1562080
20051200512082091
20012  0520
20022  121040
20032  1041560
20042200431561591
20013  0520
20023  521040
20033  1041560
20043  1562080
20053  208260

 

0

 

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

@asinusdk

 

The fact that (1) you asked for two techniques to solve the problem, and (2) the apparently artificial use of exactly 52 weeks per year, is what made me think of a homework assignment. 

 

However, put that aside.  Let me repeat the question @Reeza asked.  Is your task of such a nature that exactly 52 weeks/year satisfies what you need to do?  Or would you be better off counting, say, the number of Sundays (or Mondays ... Saturdays) in each incoming year?  Then you would get a lot of 52 "week" years, and an occasional 53 week year, which would be typical for most "real world data" problems.  Once you've answered that, other forum participants can be more helpful to you.  Here's a logical data step structure:

 

data want;
  set have;
  by id;
start=ifn(first.id,0,some_function_of(the_earliest_start));
stop=some_other_function_of(start);
if deathm=. then event=0;
else event=1;
run;

 

Now once you've confirmed how you want weeks counted, we would know how to replace the italicized expressions above.  In fact instead of some_function_of(the_earliest_start), it may be some_function_of(lag(stop)).  In your example as you put forth,

   start=ifn(first.id,0,lag(stop);

and

   stop=start+52;

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

6 REPLIES 6
Reeza
Super User
If those are weeks, what about years with 53 weeks? If you’re trying to convert a death date to week there are other ways.
asinusdk
Calcite | Level 5

May I ask why it should  be 53 weeks? Also, could you please let me know the ways?  

Reeza
Super User

365 days in a year, 7 days in a week. 

365/7 = 52.14 etc so you'll always have a portion of a 'week' left over. 

 

INTNX and INTCK will allow you manipulate date variables more easily. 

 

WEEKU()/WEEKV()/WEEKW() functions will also convert a date to a week, with slightly different methodologies. Please check the documentation. 

 


@asinusdk wrote:

May I ask why it should  be 53 weeks? Also, could you please let me know the ways?  


 

mkeintz
PROC Star

This seems like a homework assignment.  Whether or not that's the case, could you show us know what you've tried so far?

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
asinusdk
Calcite | Level 5

No. it's not homework. I only know the basic datastep procedure and proc sql (I haven't seen INTNX and INTCK mentioned above, but I have to address the real world data. That's all.) Also, I searched some procedures but I have no clue. I couldn't make any codes that's why I posted this. I wonder why you asked me it.

mkeintz
PROC Star

@asinusdk

 

The fact that (1) you asked for two techniques to solve the problem, and (2) the apparently artificial use of exactly 52 weeks per year, is what made me think of a homework assignment. 

 

However, put that aside.  Let me repeat the question @Reeza asked.  Is your task of such a nature that exactly 52 weeks/year satisfies what you need to do?  Or would you be better off counting, say, the number of Sundays (or Mondays ... Saturdays) in each incoming year?  Then you would get a lot of 52 "week" years, and an occasional 53 week year, which would be typical for most "real world data" problems.  Once you've answered that, other forum participants can be more helpful to you.  Here's a logical data step structure:

 

data want;
  set have;
  by id;
start=ifn(first.id,0,some_function_of(the_earliest_start));
stop=some_other_function_of(start);
if deathm=. then event=0;
else event=1;
run;

 

Now once you've confirmed how you want weeks counted, we would know how to replace the italicized expressions above.  In fact instead of some_function_of(the_earliest_start), it may be some_function_of(lag(stop)).  In your example as you put forth,

   start=ifn(first.id,0,lag(stop);

and

   stop=start+52;

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 6 replies
  • 908 views
  • 0 likes
  • 3 in conversation