Hi All,
My date field (Imported from Excel) written like 2/30/2015 and have $10. format. Can someone help me to convert it to 20150230 format please. Thanks,
Hi @mlogan,
I would create SAS date values from those character dates:
data _null_;
length c $10;
c='3/30/2015';
d=input(c,mmddyy10.);
format d yymmddn8.;
put d;
run;
If you need a character string in YYYYMMDD format you can apply the PUT function to the SAS date value:
dc=put(input(c,mmddyy10.),yymmddn8.);
For invalid dates the conversion will fail, though. 🙂
Hi @mlogan,
I would create SAS date values from those character dates:
data _null_;
length c $10;
c='3/30/2015';
d=input(c,mmddyy10.);
format d yymmddn8.;
put d;
run;
If you need a character string in YYYYMMDD format you can apply the PUT function to the SAS date value:
dc=put(input(c,mmddyy10.),yymmddn8.);
For invalid dates the conversion will fail, though. 🙂
You could drop the old (character) variable and rename the new (numeric) one:
data have;
length c $10;
c='3/30/2015';
run;
data want;
set have;
d=input(c,mmddyy10.);
format d yymmddn8.;
drop c;
rename d=c;
run;
If your goal is a character variable (such as DC in my previous post), you could overwrite the existing value directly, because no type conversion would occur:
data want;
set have;
c=put(input(c,mmddyy10.),yymmddn8.);
run;
However, this would leave the length of the variable at 10, although 8 would be sufficient. To avoid this, you could define a new variable with length 8 and then drop and rename as shown above.
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.