- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hello,
I would like to change the variables which are YYMMDD to MMDDYY. I have the codes below. When I tried to change one by one, everything is fine. However, after I used create macro and ran, I found out all the YYMMDD change to 01/01/1960. I have no idea why this happened. Please help, thank you.
BTW, the test is only the sample dataset. I have more variables like 'YYMMDD'. Also, I am not good at create date sample dataset. I think the code for creating the test is not very correct. If you could help me fix it too. That would be great.
data test; format scrdate YYMMDD10. scrAdmitdate YYMMDD10.; input Hstatus $1 scrdate : yymmdd10. scrAdmitdate :yymmdd10.; datalines; 1 2016-12-01 2016-12-01 2 2016-12-03 2016-12-03 3 2016-12-10 2016-12-12 4 2016-12-06 2016-12-07 ; run; proc contents data=test out=test_cont noprint; run; data new_dates; set test_cont; if format in ('YYMMDD'); run; proc sql noprint; select name into :numdvar separated by ' ' from new_dates; quit; %put &numdvar; data test_newdate; set test; array bb &numdvar; do over bb; bb=Datepart(bb); end; informat &numdvar yymmdd10.; format &numdvar mmddyy10.; run;
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@ybz12003 wrote:
I didn't know Datepart is turning the time to the Zero function.
This is plainly wrong. You do not have a TIME, you have a DATE. Seconds vs. days.
The datepart() is for turning the seconds of DATETIMES into the days of DATES.
Since you already have a DATE, the function is not needed and will (if used for dates) return a zero until 86400 days have passed since 1960-01-01.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I see the issue is with the format statement in data step test_newdate, here you are using
format &numdvar mmddyy10.;
where format is mmddyy10. if you replace mmddyy10. with yymmdd10. then you will see dates in yymmdd10.
but you need to remove the datepart code , you do not need it.
Jag
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Why do you use the datepart() function on a date?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
As you have been told many times by now (and we have to cover the issue of dates and times daily with newbies here, so you will have encountered it in other's questions), dates are counts of days and times are counts of seconds.
So the DATEPART function does this, in effect:
floor(datetime / 86400)
Applying the function to a date value (which is in the 20000s right now) will invariably end up with a result of zero, and that is the SAS anchor date of 1960-01-01.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
This is all you need. No macro is needed, no other steps are needed.
data test;
format scrdate scrAdmitdate MMddYY10.;
input Hstatus $1 scrdate : yymmdd10. scrAdmitdate :yymmdd10.;
datalines;
1 2016-12-01 2016-12-01
2 2016-12-03 2016-12-03
3 2016-12-10 2016-12-12
4 2016-12-06 2016-12-07
;
run;
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I didn't know Datepart is turning the time to the Zero function. I changed the code as below. It seems working now. Let me know if there is anything I missing.
data test_newdate; set test; array bb &numdvar; informat &numdvar yymmdd10.; format &numdvar mmddyy10.; run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@ybz12003 wrote:
I didn't know Datepart is turning the time to the Zero function.
This is plainly wrong. You do not have a TIME, you have a DATE. Seconds vs. days.
The datepart() is for turning the seconds of DATETIMES into the days of DATES.
Since you already have a DATE, the function is not needed and will (if used for dates) return a zero until 86400 days have passed since 1960-01-01.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
It looks like I didn't look the dataset very carefully. The original 'scrdate' and 'scrAdmitdate' variable did come with time. That's why the previous code using datepart function, it was used to remove the time and keep the date 'yymmdd.' However, how come it turned all to 1960-01-01?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@ybz12003 wrote:
It looks like I didn't look the dataset very carefully. The original 'scrdate' and 'scrAdmitdate' variable did come with time. That's why the previous code using datepart function, it was used to remove the time and keep the date 'yymmdd.' However, how come it turned all to 1960-01-01?
Because it was not a datetime any longer, but a date. Is this dataset a result of proc import?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content