Hello team,
Can someone let me know how to do this in a data step?
Date entered as 04/14/2021 as character to be converted to date as date number formatted: 2021-04-14
Regards,
blueblue
Let's assume you already have a dataset with a character variable with length of at least 10 bytes that contains strings like '04/14/2021'. Let's make an example.
data have;
string='04/14/2021';
run;
Now let's use that dataset to make a new dataset that also includes and actual date variable make from converting the string.
data want;
set have;
date = input(string,mmddyy10.);
format date yymmdd10.;
run;
Results:
Obs string date 1 04/14/2021 2021-04-14
Let's take a look at what we have.
2792 data _null_; 2793 set want ; 2794 put string= $quote. / string= $hex20. /; 2795 put date= comma10. / date=date9. / date=yymmdd10. / date=mmddyy10. / date=ddmmyy10. / date = hex16. /; 2796 run; string="04/14/2021" string=30342F31342F32303231 date=22,384 date=14APR2021 date=2021-04-14 date=04/14/2021 date=14/04/2021 date=40D5DC0000000000 NOTE: There were 1 observations read from the data set WORK.WANT.
try this
data sample;
datec='04/14/2021';
date=input(datec,mmddyy10.);
format date yymmdd10.;
run;
May want the yymmddd10. to have the - character instead of slash, the third d is for dash in the yymmddx format.
Let's assume you already have a dataset with a character variable with length of at least 10 bytes that contains strings like '04/14/2021'. Let's make an example.
data have;
string='04/14/2021';
run;
Now let's use that dataset to make a new dataset that also includes and actual date variable make from converting the string.
data want;
set have;
date = input(string,mmddyy10.);
format date yymmdd10.;
run;
Results:
Obs string date 1 04/14/2021 2021-04-14
Let's take a look at what we have.
2792 data _null_; 2793 set want ; 2794 put string= $quote. / string= $hex20. /; 2795 put date= comma10. / date=date9. / date=yymmdd10. / date=mmddyy10. / date=ddmmyy10. / date = hex16. /; 2796 run; string="04/14/2021" string=30342F31342F32303231 date=22,384 date=14APR2021 date=2021-04-14 date=04/14/2021 date=14/04/2021 date=40D5DC0000000000 NOTE: There were 1 observations read from the data set WORK.WANT.
Maxim 4. Try It.
Your code contains all the answers.
In that code MMDDYY is an INFORMAT and YYMMDD is a FORMAT. MMDDYY10. is the informat specification you are using with the INPUT() function. Since you set the width at 10 bytes it will only use the first 10 bytes of the character variable STRING. YYMMDD10. is the format specification you are setting as the default way to display the values of DATE.
So the result is that DATE is a numeric value that has the number of days since start of 1960 and by default it will print as 10 characters with the first four being the YEAR number then a hyphen then two digits for the month number within that year another hyphen and then two digits for the day within that month.
In SAS you use a FORMAT to convert values into text. You use an INFORMAT to convert text into values.
You can use the INPUT statement to read text into values. You can use the INPUT() function to convert text in a character string into a value.
You can use the PUT statement to write values as text. You can use the PUT() function to convert values into a character string.
You can use the FORMAT statement in a data step to permanently attach a particular format to a variable so that SAS will use that format when displaying the values by default. Similarly you can use an INFORMAT statement to attach an informat that you want SAS to use by default when reading values into the variable via and INPUT statement.
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.