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

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

 

Blue Blue
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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.

 

 

View solution in original post

6 REPLIES 6
japelin
Rhodochrosite | Level 12

try this

 

data sample;
  datec='04/14/2021';
  date=input(datec,mmddyy10.);
  format date yymmdd10.;
run;
ballardw
Super User

May want the yymmddd10. to have the - character instead of slash, the third d is for dash in the yymmddx format.

Tom
Super User Tom
Super User

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.

 

 

GN0001
Barite | Level 11
Hello,
Thanks for the response.
data want;
set have;
date = input(string,mmddyy10.);
format date yymmdd10.;
run;
is mmddyy10. format of the string?
What if I want to have date as date and formatted such as 2021-04-14?
Regards,
blue blue
Blue Blue
Tom
Super User Tom
Super User

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.

 

 

Ready to join fellow brilliant minds for the SAS Hackathon?

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!
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
  • 506 views
  • 5 likes
  • 5 in conversation