- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
do like this
data test;
CharDate="20081023";
Date=input(CharDate, yymmdd8.);
format date date9.;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Ha same answer; we had a mid-air collision. 🙂
Edit: changed colusion to collision. Reading too much newspapers lately 😉
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Great minds, you know 😉
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
i think you need date_new = input(date, yymmdd10.); instead of yymmdd8.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content