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!
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;
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;
Thank you very much! Worked like a charm!
@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
@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.
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!
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.
Ready to level-up your skills? Choose your own adventure.