DATA Step, Macro, Functions and more

How to convert character format yyyymmdd to a date format ?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 9
Accepted Solution

How to convert character format yyyymmdd to a date format ?

In my date, date is yyyymmdd (for example 20081023) and I want to convert it to a date format (such as 23/10/2008) and also know which weekday. How can I do that? I read another similar post about converting and tried: 

 

data withdate;
   date_new = input(date, yymmdd8.);
   format date_new weekdate9. ;
run;

 

but it doesn't work.

 

Really appreciate your suggestions!


Accepted Solutions
Solution
‎05-02-2018 12:48 PM
Super User
Posts: 8,218

Re: How to convert character format yyyymmdd to a date format ?

Posted in reply to mkt_apprentice

Then try:

data want_where_num;
  set have_with_num_date;
  date_new = input(put(date,8.), yymmdd8.);
  format date_new weekdate9. ;
run;

Art, CEO, AnalystFinder.com

 

View solution in original post


All Replies
PROC Star
Posts: 1,402

Re: How to convert character format yyyymmdd to a date format ?

Posted in reply to mkt_apprentice

do like this

 

data test;
   CharDate="20081023";
   Date=input(CharDate, yymmdd8.);
   format date date9.;
run;
Valued Guide
Posts: 540

Re: How to convert character format yyyymmdd to a date format ?

[ Edited ]

Ha same answer; we had a mid-air collision. :-)

 

Edit: changed colusion to collision. Reading too much newspapers lately ;-)

PROC Star
Posts: 1,402

Re: How to convert character format yyyymmdd to a date format ?

Posted in reply to jklaverstijn

Great minds, you know Smiley Wink

Valued Guide
Posts: 540

Re: How to convert character format yyyymmdd to a date format ?

Posted in reply to mkt_apprentice

How does it not woek? Can you provide us with a log?

If I run your code it does work:

data withdate;
   date = '20081023';
   date_new = input(date, yymmdd8.);
   format date_new weekdate9. ;
   put _all_;
run;

Do note that I gave your variable date a value as per your question. Otherwise I can understand it doesn't work. This is the log from my code:

1          data withdate;
2             date = '20081023';
3             date_new = input(date, yymmdd8.);
4             format date_new weekdate9. ;
5             put _all_;
6          run;

date=20081023 date_new=Thursday _ERROR_=0 _N_=1
NOTE: The data set WORK.WITHDATE has 1 observations and 2 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds

If this doesn't clarify things for you please add your log and your desired results.

 

Hope this helps,

- Jan.

Occasional Contributor
Posts: 9

Re: How to convert character format yyyymmdd to a date format ?

Posted in reply to jklaverstijn
Yes, 20081023 is only an example. Date is a variable. How can I change the whole variable or create a new variable to have a format of mm/dd/yyyy or show week day?
Occasional Contributor
Posts: 9

Re: How to convert character format yyyymmdd to a date format ?

Posted in reply to jklaverstijn
My log said:
NOTE: Mathematical operations could not be performed at the following places. The results of the
operations have been set to missing values.
Each place is given by: (Number of times) at (Line)Smiley SadColumn).
121608 at 147:13
NOTE: There were 121608 observations read from the data set ORGIN.DATE.
NOTE: The data set ORIGIN.DATE1 has 121608 observations and 356 variables.
NOTE: At least one W.D format was too small for the number to be printed. The decimal may be
shifted by the "BEST" format.
NOTE: DATA statement used (Total process time):
real time 1.64 seconds
cpu time 0.87 seconds

Super User
Posts: 2,061

Re: How to convert character format yyyymmdd to a date format ?

Posted in reply to mkt_apprentice

i think you need   date_new = input(date, yymmdd10.); instead of yymmdd8.

Occasional Contributor
Posts: 9

Re: How to convert character format yyyymmdd to a date format ?

Posted in reply to novinosrin
Date is 20081023 so there are 8 spaces. 2008/10/23 would be a yymmdd10.
Super User
Posts: 24,012

Re: How to convert character format yyyymmdd to a date format ?

Posted in reply to mkt_apprentice

Is your current variable a numeric or character variable and what is the format?

If you're not sure, run a PROC CONTENTS on your data set.

 

proc contents data=sashelp.class;run;

@mkt_apprentice wrote:
Date is 20081023 so there are 8 spaces. 2008/10/23 would be a yymmdd10.

 

Occasional Contributor
Posts: 9

Re: How to convert character format yyyymmdd to a date format ?

It says Date is Num Type, Len 8, Format F8.
Solution
‎05-02-2018 12:48 PM
Super User
Posts: 8,218

Re: How to convert character format yyyymmdd to a date format ?

Posted in reply to mkt_apprentice

Then try:

data want_where_num;
  set have_with_num_date;
  date_new = input(put(date,8.), yymmdd8.);
  format date_new weekdate9. ;
run;

Art, CEO, AnalystFinder.com

 

Occasional Contributor
Posts: 9

Re: How to convert character format yyyymmdd to a date format ?

It works! Thanks a lot!
Super User
Posts: 24,012

Re: How to convert character format yyyymmdd to a date format ?

Posted in reply to mkt_apprentice

Then you have a numeric value that first needs to be converted to a SAS numeric variable. 

 

SAS stores dates as a number, which is the number of days from January 1, 1960. Then to show a formatted value a format is applied. 

One of the simplest ways to do this is to convert the number to a character and then read it back in as a date and then apply the format. 


So you would use PUT() to convert it to a character and then use INPUT() with the YYMMDD format to read it back in correctly. Then you would apply the format to have it show up the way you want. 

 

@art297 code does exactly that. 

 

Occasional Contributor
Posts: 9

Re: How to convert character format yyyymmdd to a date format ?

Thank you! I not only ran it successfully but also understood the gap in my SAS understanding Smiley Happy
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 15 replies
  • 529 views
  • 3 likes
  • 7 in conversation