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
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.
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:
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.
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.
Ready to level-up your skills? Choose your own adventure.