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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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

15 REPLIES 15
PeterClemmensen
Tourmaline | Level 20

do like this

 

data test;
   CharDate="20081023";
   Date=input(CharDate, yymmdd8.);
   format date date9.;
run;
jklaverstijn
Rhodochrosite | Level 12

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

 

Edit: changed colusion to collision. Reading too much newspapers lately 😉

jklaverstijn
Rhodochrosite | Level 12

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.

mkt_apprentice
Obsidian | Level 7
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?
mkt_apprentice
Obsidian | Level 7
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):(Column).
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

novinosrin
Tourmaline | Level 20

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

mkt_apprentice
Obsidian | Level 7
Date is 20081023 so there are 8 spaces. 2008/10/23 would be a yymmdd10.
Reeza
Super User

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.

 

mkt_apprentice
Obsidian | Level 7
It says Date is Num Type, Len 8, Format F8.
art297
Opal | Level 21

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

 

mkt_apprentice
Obsidian | Level 7
It works! Thanks a lot!
Reeza
Super User

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. 

 

mkt_apprentice
Obsidian | Level 7
Thank you! I not only ran it successfully but also understood the gap in my SAS understanding 🙂

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 15 replies
  • 57516 views
  • 6 likes
  • 7 in conversation