How to change the number to date?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 7
Accepted Solution

How to change the number to date?

Hello every SAS users, here is a very simple question and i need your help!!

I want to let the records with T2DM=0 change its Diag_day to 01/01/1998.

But I meet some problem while programming it.

Can anyone help me solve the problem?

Thanks!!!!!!

data test4;

set test3;

if T2DM=0 then Diag_day_Q=19980101;

if T2DM=0 then Diag_day=mdy(substr(Diag_day_Q,5,2), substr(Diag_day_Q,7,2),substr(Diag_day_Q,1,4));

format Diag_day mmddyy10.;

run;

NOTE: Numeric values have been converted to character values at the places given by: (Line)Smiley SadColumn).

      16841:36   16841:60   16841:83

NOTE: Character values have been converted to numeric values at the places given by: (Line)Smiley SadColumn).

      16841:29   16841:53   16841:76

NOTE: Missing values were generated as a result of performing an operation on missing values.

      Each place is given by: (Number of times) at (Line)Smiley SadColumn).

      183960 at 16841:25

NOTE: There were 386182 observations read from the data set WORK.TEST3.

NOTE: The data set WORK.TEST4 has 386182 observations and 52 variables.

NOTE: DATA statement used (Total process time):

      real time           0.45 seconds

      cpu time            0.39 second


Accepted Solutions
Solution
‎02-14-2014 01:56 AM
Trusted Advisor
Posts: 1,129

Re: How to change the number to date?

Please try the below code

data test4;

set test3;

if T2DM=0 then Diag_day_Q="19980101";

if T2DM=0 then Diag_day=mdy(input(substr(Diag_day_Q,5,2),8.), input(substr(Diag_day_Q,7,2),8.), input(substr(Diag_day_Q,1,4),8.));

format Diag_day mmddyy10.;

run;

From the above, no notes in the log will be generated. Used character data in substr functions. Please check the Bold letters.

Tried your code, we will get the output but with notes.

To avoid it, use the character data in substr and compress function, instead of the numeric data.

Thanks,

Jag

Thanks,
Jag

View solution in original post


All Replies
Contributor
Posts: 74

Re: How to change the number to date?

i think this will work

data test4;

format Diag_day date10.;

Diag_day_Q = 19980101;

Diag_day = mdy(substr(compress(Diag_day_Q),5,2), substr(compress(Diag_day_Q),7,2),substr(compress(Diag_day_Q),1,4));

run;

Solution
‎02-14-2014 01:56 AM
Trusted Advisor
Posts: 1,129

Re: How to change the number to date?

Please try the below code

data test4;

set test3;

if T2DM=0 then Diag_day_Q="19980101";

if T2DM=0 then Diag_day=mdy(input(substr(Diag_day_Q,5,2),8.), input(substr(Diag_day_Q,7,2),8.), input(substr(Diag_day_Q,1,4),8.));

format Diag_day mmddyy10.;

run;

From the above, no notes in the log will be generated. Used character data in substr functions. Please check the Bold letters.

Tried your code, we will get the output but with notes.

To avoid it, use the character data in substr and compress function, instead of the numeric data.

Thanks,

Jag

Thanks,
Jag
Frequent Contributor
Posts: 83

Re: How to change the number to date?

This seems like a rather roundabout way to define a date, why not just write

data test4;

set test3;

if T2DM=0 then Diag_day='01JAN1998'd;

format Diag_day mmddyy10.;

run;

Essentially if T2DM is equal to 0 then diag_day is converted to the SAS date for 01/01/1998

Also this way you don't have to create an extra variable in Diag_day_Q which you likely will have to delete in the future, or have to make sure that your substr commands are all in the right places.

For reference you can always directly specify a date by putting it into the DDMMMYYYY format wrapping it in ' and putting a d after it.

☑ This topic is SOLVED.

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

Discussion stats
  • 3 replies
  • 382 views
  • 3 likes
  • 4 in conversation