Desktop productivity for business analysts and programmers

(Urgent) Please Help: creating a new date variable in the data set

Accepted Solution Solved
Reply
Contributor
Posts: 24
Accepted Solution

(Urgent) Please Help: creating a new date variable in the data set

Hello,

 

I am simply trying to create a new date variable in my cancer survival data set to mark the end of the observation date as 2015-06-30.

The new date variable named "obs_fn" should all have the same date: 2015-06-30.

 

I have tried the following:

 

data w.cancer;

set w.cancer;

format obs_fn yymmddn8.;

obs_fn = 20160630;

run;

 

The new variable "" appears as 9999-12-31

 

I am not sure what I have done wrong.

 

Is there a way to make this work correctly in SAS enterpirse?

 

Thank you.


Accepted Solutions
Solution
‎07-20-2016 08:22 PM
Super Contributor
Posts: 414

Re: (Urgent) Please Help: creating a new date variable in the data set

Smiley Happy Haha yes three left turns instead of on right turn. In that case why not do:

 

obs_dn=20160630;
obs_dn=mdy(floor(mod(obs_dn,10000)/100), mod(obs_dn,100), round(obs_dn/10000));

And many more approaches are thinkable.

View solution in original post


All Replies
Super Contributor
Posts: 414

Re: (Urgent) Please Help: creating a new date variable in the data set

I ansered this in your other post. Somehow this got posted twice:

 

SAS has the concept of the date literal. Just write the desired date in DATE9. forat between quotes and a trailing d:

 

 

obs_fn = '30JUN2016'd;

 

Contributor
Posts: 24

Please Help: creating a new date variable in the data set

Hello, I am simply trying to create a new date variable in my cancer survival data set to mark the end of the observation date as 2015-06-30. The new date variable named "obs_fn" should all have the same date: 2015-06-30. I have tried the following:

 

data w.cancer;

set w.cancer;

format obs_fn yymmddn8.;

obs_fn = 20160630;

run;

 

The new variable "" appears as 9999-12-31 I am not sure what I have done wrong. Is there a way to make this work correctly in SAS?

 

Thank you.

Super Contributor
Posts: 414

Re: Please Help: creating a new date variable in the data set

SAS has the concept of the date literal. Just write the desired date in DATE9. forat between quotes and a trailing d:

 

obs_fn = '30JUN2016'd;

That should do the trick.

 

Similar notations are available for time, datatime hex, etc.

 

Hope this helps,

- Jan.

Contributor
Posts: 60

Re: (Urgent) Please Help: creating a new date variable in the data set

you can use below if you don't want to use date9. format and 'YYYYMMDD' d things : 

 

data ds1;
input a b;
datalines;
1 2
3 4
5 6
;

run;

data want;
set ds1;
format obs_dn yymmdd10.;
informat obs_dn yymmdd8.;
obs_dn = 20160630;
obs_dn=input(put(obs_dn,8.),yymmdd8.);
run;

proc print data=want;
run;

Solution
‎07-20-2016 08:22 PM
Super Contributor
Posts: 414

Re: (Urgent) Please Help: creating a new date variable in the data set

Smiley Happy Haha yes three left turns instead of on right turn. In that case why not do:

 

obs_dn=20160630;
obs_dn=mdy(floor(mod(obs_dn,10000)/100), mod(obs_dn,100), round(obs_dn/10000));

And many more approaches are thinkable.

Super Contributor
Posts: 414

Re: (Urgent) Please Help: creating a new date variable in the data set

To elaborate on your embedded second question:

 


...
format obs_fn yymmddn8.;

obs_fn = 20160630;

run;

 

The new variable "" appears as 9999-12-31

 

I am not sure what I have done wrong.

 

SAS stores dates internally as an integer enumerating the number of days since (or up to) January 1st, 1960. You have assigned the integer value 20160630 to your date value. This is then interpreted as 20160630 days since 01JAN1960, apparently being so high that we reach the end of the current 10.000 year period (10 kilo annum), 31DEC999.

 

Hope this helps,

- Jan

Contributor
Posts: 24

Re: (Urgent) Please Help: creating a new date variable in the data set

Thank you so much for your answer.

 

It really helped me a lot.

 

May I ask you one more question?

 

Is there a way to add the date variable conditionally?

 

For example, how should I add the date variable "OBS_FN" to those who survive? (1=death, 0=survival)

 

My data looks like the following:

 

cancer therapy data

 

PATIENT_ID  FIRST THERAPY  DEATH    DEATH_DATE     

100012              2012-01-01               1            2012-05-05

100013               2012-02-15              0                     .         

 

From your help, I have tried the following but it addes the OBS_FN regardless of the death or surival.

 

data w.cancer; set cancer;

IF DEATH=0 THEN;

format OBS_FN yymmddn8.;

obs_fn = 20150630;

obs_fn = input(put(obs_fn,8.),yymmdd8.)

run;

 

which resulted in:

 

PATIENT_ID  FIRST THERAPY  DEATH    DEATH_DATE      OBS_FN       

100012              2012-01-01               1            2012-05-05      2015-06-30

100013               2012-02-15              0                                     2015-06-30

 

 

I want to make the data look like this:

 

PATIENT_ID  FIRST THERAPY  DEATH    DEATH_DATE      OBS_FN       

100012              2012-01-01               1            2012-05-05

100013               2012-02-15              0                                     2015-06-30

        

.

.

I would really appreciate any kind of advice or help.

 

Thank you                

Super Contributor
Posts: 414

Re: (Urgent) Please Help: creating a new date variable in the data set

[ Edited ]

Hi,

 

Use

 

if death=0 then obs_fn="30JUN2015"d;

If the condition is not met the variable will get the missing value.

 

Also I notice you have adopted the solution of @atul_desh to your initial question. Although correct it is also unnescecariliy complicated and waistful. I suggest you use the much simpler "30JUN2015"d notation instead.

 

The line

if death=0 then;

is not syntactically incorrect, it just does nothing. In this case the semicolon is interpreted as a NULL statement. Therefore the value is assigned to all rows instead of to just the ones you were hoping it would.

 

Hope this helps,

- Jan.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 432 views
  • 0 likes
  • 3 in conversation