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

Hi All, 

 

I'm having a hard time converting a mm/dd/yy format variable to the SAS date9 DDMMMYY format. 

 

I have a variable, Admit, that is in mm/dd/yy format. I've tried this code:

data admit_test;
set t.dch_cj_new;
new_admit = input(Admit, date9.);
run;

Per this support page (https://support.sas.com/kb/24/590.html) and the video where Anna transforms her race data, but my data does not appear in the correct format:

bazingarollcall_0-1625840488683.png

What am I doing wrong here?

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Run Proc contents on the data set and see what type Admin is. If the variable is numeric it appears likely to have a SAS mmddyy10 format applied. If so then all you would do is apply the format(which you have not done).

If it is character then the input should use MMDDYY10. instead of Date9. Date9 is used to read values like 05JUL2021, not 07/05/2021

View solution in original post

5 REPLIES 5
ballardw
Super User

Run Proc contents on the data set and see what type Admin is. If the variable is numeric it appears likely to have a SAS mmddyy10 format applied. If so then all you would do is apply the format(which you have not done).

If it is character then the input should use MMDDYY10. instead of Date9. Date9 is used to read values like 05JUL2021, not 07/05/2021

bazingarollcall
Fluorite | Level 6
Awesome - this worked! I added a format statement and everything showed up.

What if I have another date variable in numeric 05Jul2021 format and I need it to be in the mmddyy format?
dob = input(Date_of_Birth, date9.);
format dob date9.;
run;

This does the same thing as before and produces the column but it contains no values.

PaigeMiller
Diamond | Level 26

@bazingarollcall wrote:
Awesome - this worked! I added a format statement and everything showed up.

What if I have another date variable in numeric 05Jul2021 format and I need it to be in the mmddyy format?
dob = input(Date_of_Birth, date9.);
format dob date9.;
run;

This does the same thing as before and produces the column but it contains no values.

If it truly is numeric and appears as 05JUL2021, then you don't need the INPUT() function, which converts character to numeric (and you already have a numeric). Just issue a FORMAT statement in a DATA step or PROC.

 

format date_of_birth date9.;

 

--
Paige Miller
bazingarollcall
Fluorite | Level 6
Thank you! This worked!
ballardw
Super User

If the variable is numeric and appearing in DATE9. format the only change needed is a Format statement with the new format.

 

Format dob mmddyy10.; for example.

 

Formats are rules to display values, not the values themselves.

The INPUT function expects character inputs. So if the variable is already numeric there is an internal conversion to character that SAS uses. The log will typically show a message about "numeric values were converted to character at Line:position".

The default conversion that SAS applies to a numeric variable in this case will typically contain leading spaces for dates because of the number of positions it creates and the range of the underlying numeric value. So INPUT sees a value starting with spaces which it treats as invalid characters for the Informat specified. Which creates missing values.

 

Proc contents is your friend to examine your variables. You need to know whether a variable is numeric or character before using INPUT function. Then match the informat to character value.

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
  • 1065 views
  • 2 likes
  • 3 in conversation