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.
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.
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;
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.
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.
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;
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.
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
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
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.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
Check out this tutorial series to learn how to build your own steps in SAS Studio.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.