BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
chinna0369
Pyrite | Level 9

Hi all,

 

I have different formats of values in character date variable. I am unable to convert it to numeric. Can anyone help me with the solution. I tried anydtdt and other formats too. But no luck.

 

data have;
input date $19.;
datalines;
21329
22614
22636
2022-02-21
2022-03-15
2022-04-29
;
run;

data want;
set have;
want=input(date,yymmdd10.);
run;

Thanks in advance!

 

Ad

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

What dates are supposed to be represented by the first 3 values?

 

You can conditionally execute the input function.

data want;
   set have;
   if length (date)=10 then want=input(date,yymmdd10.);
   if length (date)=5 then want = input(date,f5.);
   format want date9.;
run;

So this code only attempts to use the yymmdd10 format with the string value is 10 characters.

The second bit is guessing that someone may have done something very odd and managed to convert the SAS date numeric values to character somewhere and this reads them back to a numeric value and is then formatted. If that is not the case you really have to tell us what date is represented by 21329 as it may require some careful parsing.

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

If those two patterns are all you have then just check which one you have and use the right code to each.

data have;
input string $19.;
datalines;
21329
22614
22636
2022-02-21
2022-03-15
2022-04-29
;

data want;
  set have;
  if index(string,'-') then date=input(string,yymmdd10.);
  else date=input(string,32.);
  format date yymmdd10.;
run;

Results:

Tom_0-1655175270668.png

 

ballardw
Super User

What dates are supposed to be represented by the first 3 values?

 

You can conditionally execute the input function.

data want;
   set have;
   if length (date)=10 then want=input(date,yymmdd10.);
   if length (date)=5 then want = input(date,f5.);
   format want date9.;
run;

So this code only attempts to use the yymmdd10 format with the string value is 10 characters.

The second bit is guessing that someone may have done something very odd and managed to convert the SAS date numeric values to character somewhere and this reads them back to a numeric value and is then formatted. If that is not the case you really have to tell us what date is represented by 21329 as it may require some careful parsing.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 835 views
  • 0 likes
  • 3 in conversation