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

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?

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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.

View solution in original post

2 REPLIES 2
ballardw
Super User

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.

Tom
Super User Tom
Super User

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.

 

 

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

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!

Register Now

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
  • 2 replies
  • 4540 views
  • 0 likes
  • 3 in conversation