I have variable, test date, that is written as "2021-01-01" and was formatted as a character variable in the file I have. How can I format it to be a date variable, 01012021?
One creating a new variable with an INPUT statement and assign appropriate format.
data example; x="2021-01-01" ; y= input(x,yymmdd10.); format y mmddyyn8.; run;
There are a large number of SAS supplied date formats and you can roll your own with Proc Format for date values if you can't find one you like/want. the N in the MMDDYYN is to indicate "no separator" between month, day and year.
I have to guess that you want Month first as your shown example of 01012021 does not clearer indicate if the first position is a month or day value. If by chance you want day first there is a similar DDMMYYN format available.
One creating a new variable with an INPUT statement and assign appropriate format.
data example; x="2021-01-01" ; y= input(x,yymmdd10.); format y mmddyyn8.; run;
There are a large number of SAS supplied date formats and you can roll your own with Proc Format for date values if you can't find one you like/want. the N in the MMDDYYN is to indicate "no separator" between month, day and year.
I have to guess that you want Month first as your shown example of 01012021 does not clearer indicate if the first position is a month or day value. If by chance you want day first there is a similar DDMMYYN format available.
A FORMAT in SAS is instructions for how to display values as text. There is no FORMAT that will display a character string as a date.
You could use an INPUT() function with an INFORMAT (informats are instructions for converting text into values) to make a new variable that has a date value. You could then attach a format to that to have it print in the style you want.
actual_date=input('test date'n,yymmdd10.);
format actual_date mmddyyn8. ;
If you would rather replace the text string in your 'test date'n variable then you could use the PUT() function with the MMDDYYn8. format to to store a string in MMDDYYYY style back into the original character variable. But it wouldn't be a date value.
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.