I am trying to convert date to SAS date instead of doing the below:
data b2;
set b1;
format_date1 = input(put(DOB1, z8.),yymmdd8.);
format_date2 = input(put(DOB2, z8.),yymmdd8.);
format_date3 = input(put(DOB3, z8.),yymmdd8.);
format_date4 = input(put(DOB4, z8.),yymmdd8.);
format_date5 = input(put(DOB5, z8.),yymmdd8.);
format format_date1 format_date2 format_date3
format_date4 format_date5 mmddyy10.;
drop DOB1 DOB2 DOB3 DOB4 DOB5;
rename format_date1 = DOB1 format_date2 = DOB2
format_date3 = DOB3 format_date4 = DOB4 format_date5 = DOB5;
run;
So you have numeric variables named DOB1 to DOB5 that have numbers like 20,231,003 and you want to convert them to actual DATE values instead?
What is wrong with the code you have? Does it work? Or not? If not what is wrong?
Are you looking for an easier way?
data b2;
set b1;
array dob dob1-dob5;
do over dob;
dob=input(put(DOB, z8.),yymmdd8.);
end;
format dob1-dob5 yymmdd10.;
run;
The date looks like this in the data: 19820915 (sorry, forgot to put that in)
@bhca60 wrote:
It doesnt work - doesnt ouput anything
That doesn't tell us much. You need to share the SAS log for such issues as well as a proc contents of your source data.
Below some sample code how to convert your data both for when the source is stored in a numerical or character variable.
data have;
num_var =19820915;
char_var='19820915';
run;
data want;
set have;
format dt1 dt2 date9.;
dt1=input(put(num_var,8.), yymmdd8.);
dt2=input(char_var,yymmdd8.);
run;
proc print data=want;
run;
data b2;
set b1;
SAS_DATE1 = PUT(DOB1, $10.);
b_DOB1_SAS_DATE = INPUT(SAS_DATE1, 10.);
SAS_DATE2 = PUT(DOB2, $10.);
b_DOB2_SAS_DATE = INPUT(SAS_DATE2, 10.);
SAS_DATE3 = PUT(DOB3, $10.);
b_DOB3_SAS_DATE = INPUT(SAS_DATE3, 10.);
SAS_DATE4 = PUT(DOB4, $10.);
b_DOB4_SAS_DATE = INPUT(SAS_DATE4, 10.);
SAS_DATE5 = PUT(DOB5, $10.);
b_DOB5_SAS_DATE = INPUT(SAS_DATE5, 10.);
run;
This brings in dob1 and populates as for example, 2023-09-16, but the other dates dob2-dob5 are coming in blank even though there are dates in those fields.
That doesn't make much sense.
Is DOB1 numeric or character? If it is numeric why use a character format? If it is character why use the PUT() function at all?
And why use the numeric informat if you want to make a DATE value? Having a date value stored as a number like 20,131,003 does not do you much good. You want that date to be stored as the number 23,286 (the number of days since 1960) so that you can do date type calculations with it and use date type formats to display it the way you want.
104 data _null_; 105 dt = '03OCT2023'd ; 106 put dt = comma7. dt=date9. dt=yymmdd10.; 107 run; dt=23,286 dt=03OCT2023 dt=2023-10-03
It looks like you wanted to convert to SAS date from char format. Assuming all DOB variables have precisely a length of 8 characters, then your original codes should work. Check dob2-dob5 format if the output is blank.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.