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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

5 REPLIES 5
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
sy12345
Fluorite | Level 6

Thank you very much! Worked like a charm!

data_null__
Jade | Level 19

@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 

ballardw
Super User

@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.

 

sy12345
Fluorite | Level 6
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!

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 50750 views
  • 4 likes
  • 4 in conversation