DATA Step, Macro, Functions and more

How to convert numeric value to SAS date

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

How to convert numeric value to SAS date

[ Edited ]

Hello, 

 

I am using SAS 9.3 and I have a variable "DATEOFDIAGNOSIS". When I look in the Column Attributes of the Viewtable, it has a format of BEST12. informat of BEST32., length is 8, and type is numeric (Example: 20090930). I would like to change this variable into a SAS date value, so I can use it to do date calculations. I want to have it in date9. informat. 

I tried a bunch of different code after researching and in some cases, only some of the dates converted and others were left blank. 

 

I tried the following codes and did not work: 

 

data demo2;
set demo;
DATEOFDIAGNOSIS2 = input (put(DATEOFDIAGNOSIS, 12.), yymmdd8.);
format DATEOFDIAGNOSIS2 YYMMDD8.;
run;

 

data radtumorreg3;
set radtumorreg2;
char_var = put(DATEOFDIAGNOSIS,12.);
sas_date = input (char_var, mmddyy8.);
format sas_date date8.;
run;

 

Thank you!


Accepted Solutions
Solution
‎03-13-2017 01:15 PM
Super User
Super User
Posts: 7,942

Re: How to convert numeric value to SAS date

As you will see on every post here, post some test data in the form of a datastep.  The code I provide below works fine, so what "does not work"?

data have;
  date_num=20090930;
  date_var=input(put(date_num,best8.),yymmdd8.);
  format date_var date9.;
run;

View solution in original post


All Replies
Solution
‎03-13-2017 01:15 PM
Super User
Super User
Posts: 7,942

Re: How to convert numeric value to SAS date

As you will see on every post here, post some test data in the form of a datastep.  The code I provide below works fine, so what "does not work"?

data have;
  date_num=20090930;
  date_var=input(put(date_num,best8.),yymmdd8.);
  format date_var date9.;
run;
New Contributor
Posts: 4

Re: How to convert numeric value to SAS date

Thank you very much! Worked like a charm!

Respected Advisor
Posts: 3,799

Re: How to convert numeric value to SAS date


sy12345 wrote:

Hello, 

 

I am using SAS 9.3 and I have a variable "DATEOFDIAGNOSIS". When I look in the Column Attributes of the Viewtable, it has a format of BEST12. informat of BEST32., length is 8, and type is numeric (Example: 20090930). I would like to change this variable into a SAS date value, so I can use it to do date calculations. I want to have it in date9. informat. 

I tried a bunch of different code after researching and in some cases, only some of the dates converted and others were left blank. 

 

I tried the following codes and did not work: 

 

data demo2;
set demo;
DATEOFDIAGNOSIS2 = input (put(DATEOFDIAGNOSIS, 12.), yymmdd8.);
format DATEOFDIAGNOSIS2 YYMMDD8.;
run;

 

data radtumorreg3;
set radtumorreg2;
char_var = put(DATEOFDIAGNOSIS,12.);
sas_date = input (char_var, mmddyy8.);
format sas_date date8.;
run;

 

Thank you!


The problem is you are using w=12 in the PUT and that is RIGHT justified.  When you read CHAR_VAR with MMDDYY8 it only reads the first 8 characters 4 of which are blank.

 

Change 12 to 8 or use the justification modifier 12.-L 

Super User
Posts: 11,343

Re: How to convert numeric value to SAS date


sy12345 wrote:

Hello, 

 

I am using SAS 9.3 and I have a variable "DATEOFDIAGNOSIS". When I look in the Column Attributes of the Viewtable, it has a format of BEST12. informat of BEST32., length is 8, and type is numeric (Example: 20090930). I would like to change this variable into a SAS date value, so I can use it to do date calculations. I want to have it in date9. informat. 

I tried a bunch of different code after researching and in some cases, only some of the dates converted and others were left blank. 

 

I tried the following codes and did not work: 

 

data demo2;
set demo;
DATEOFDIAGNOSIS2 = input (put(DATEOFDIAGNOSIS, 12.), yymmdd8.);
format DATEOFDIAGNOSIS2 YYMMDD8.;
run;

 

data radtumorreg3;
set radtumorreg2;
char_var = put(DATEOFDIAGNOSIS,12.);
sas_date = input (char_var, mmddyy8.);
format sas_date date8.;
run;

 

Thank you!


If you have 8 digits why did you put 12? That adds 4 blank spaces at the begining of the character value. The mmddyy Informat starts reading at the first position of the character value which is blank and hence invalid.

 

New Contributor
Posts: 4

Re: How to convert numeric value to SAS date

I didn't know about that. I just saw that the format was in "BEST12." so I thought I had to match that. Thank you for the info though!
☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 763 views
  • 3 likes
  • 4 in conversation