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!
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
do like this
data test;
CharDate="20081023";
Date=input(CharDate, yymmdd8.);
format date date9.;
run;
Ha same answer; we had a mid-air collision. 🙂
Edit: changed colusion to collision. Reading too much newspapers lately 😉
Great minds, you know 😉
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.
i think you need date_new = input(date, yymmdd10.); instead of yymmdd8.
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.
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
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.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.