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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
jklaverstijn
Rhodochrosite | Level 12

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

8 REPLIES 8
jklaverstijn
Rhodochrosite | Level 12

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;

 

sasworker16
Calcite | Level 5

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.

jklaverstijn
Rhodochrosite | Level 12

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.

atul_desh
Quartz | Level 8

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;

jklaverstijn
Rhodochrosite | Level 12

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.

jklaverstijn
Rhodochrosite | Level 12

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

sasworker16
Calcite | Level 5

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                

jklaverstijn
Rhodochrosite | Level 12

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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