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

Hi,

I have a date variable that is numeric ((02JAN2018) ) with date9. format and date 9 informat  length 8.

I would like it to be character date yymmdd10.

 

Your help will be appreciated

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

There is no such thing as a character date in SAS. You can convert the date to a plain old character string, which is only useful in labels and titles and reports. If you care going to do additional computations or work with the date, you need to leave it as numeric.

 

Here is the conversion to character:

 

char_date = put(date,yymmdd10.);

Perhaps you meant that you want to leave the variable as numeric but with a different format? That would be much more useful and reasonable than creating a character string. In that case, just change the format. 

 

Editor's note: 
As PaigeMiller noted, converting the date to a character string is different than reformatting the date with a format that uses characters.  If reformatting is the goal, Astounding provides example code that can be used in a Proc step or a DATA step.

format datevar yymmdd10.;

 

--
Paige Miller

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

There is no such thing as a character date in SAS. You can convert the date to a plain old character string, which is only useful in labels and titles and reports. If you care going to do additional computations or work with the date, you need to leave it as numeric.

 

Here is the conversion to character:

 

char_date = put(date,yymmdd10.);

Perhaps you meant that you want to leave the variable as numeric but with a different format? That would be much more useful and reasonable than creating a character string. In that case, just change the format. 

 

Editor's note: 
As PaigeMiller noted, converting the date to a character string is different than reformatting the date with a format that uses characters.  If reformatting is the goal, Astounding provides example code that can be used in a Proc step or a DATA step.

format datevar yymmdd10.;

 

--
Paige Miller
Dhana18
Obsidian | Level 7

Thank you for your response, i would like to create a new character variable with yymmdd10. format.

Dhana18
Obsidian | Level 7

Thank you! I just changed the format and it worked!

PeterClemmensen
Tourmaline | Level 20
data test;   
   Date='02JAN2018'd;
   CharDate=put(Date, yymmdd10.);
   format Date date9.;
run;

However, not a good idea to have dates stored as character...

Astounding
PROC Star

If you are not happy with the DATE9 format, just use a different format:

 

format datevar yymmdd10.;

 

You can add that statement in a PROC step, in which case the format is used just for the duration of the procedure.

 

Or you can add it in a DATA step, in which case the format becomes the default format going forward from that point.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 46038 views
  • 3 likes
  • 4 in conversation