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

I have character dates and converting them to num date9. but fails to work for char date which is like "2 FEB 2021".

char_date

22 DEC 2020
2 FEB 2021

i use the below step and it works for first record but doesnt work for second one.

ADT=input(char_date, date9.);

 

How do i get 0 in case day is single digit.

Any clue please

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

To get a leading zero, either use a custom format for DATE9, or use another standard format:

data have;
input char_date $20.;
datalines;
22 DEC 2020
2 FEB 2021
;

proc format;
picture mydate
  other='%0d%b%Y' (datatype=date language=english)
;
run;
  
data want;
set have;
num_date = input(compress(char_date),date9.);
format num_date mydate9.;
run;

or 

data want;
set have;
num_date = input(compress(char_date),date9.);
format num_date yymmdd10.;
run;

 

View solution in original post

6 REPLIES 6
japelin
Rhodochrosite | Level 12

For example, if you do this and remove the spaces, it will be recognized.

 

  ADT=input(compress(char_date), date9.);
noda6003
Quartz | Level 8

No, unfortunately it didnt work

japelin
Rhodochrosite | Level 12

can you provide data that can be reproduced using datalines in the data step?

Kurt_Bremser
Super User

To get a leading zero, either use a custom format for DATE9, or use another standard format:

data have;
input char_date $20.;
datalines;
22 DEC 2020
2 FEB 2021
;

proc format;
picture mydate
  other='%0d%b%Y' (datatype=date language=english)
;
run;
  
data want;
set have;
num_date = input(compress(char_date),date9.);
format num_date mydate9.;
run;

or 

data want;
set have;
num_date = input(compress(char_date),date9.);
format num_date yymmdd10.;
run;

 

Ksharp
Super User
data want;
set have;
num_date = input(char_date,date11.);
format num_date date9.;
run;
Tom
Super User Tom
Super User

Tell the INPUT() function to use the full length of the string instead of truncating at 9 characters.

data have;
  input char_date $11.;
cards;
22 DEC 2020
2 FEB 2021
;

data want;
  set have;
  date = input(char_date,date11.);
  format date date9. ;
run;
Obs     char_date          date

 1     22 DEC 2020    22DEC2020
 2     2 FEB 2021     02FEB2021

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
  • 6 replies
  • 2643 views
  • 4 likes
  • 5 in conversation