SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ybz12003
Rhodochrosite | Level 12

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

@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.

View solution in original post

9 REPLIES 9
Jagadishkatam
Amethyst | Level 16

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.

Thanks,
Jag
ChrisNZ
Tourmaline | Level 20

Why do you use the datepart() function on a date?

Kurt_Bremser
Super User

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.

PaigeMiller
Diamond | Level 26

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
ybz12003
Rhodochrosite | Level 12

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;

Kurt_Bremser
Super User

@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.

ybz12003
Rhodochrosite | Level 12

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?

Kurt_Bremser
Super User

@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?

ybz12003
Rhodochrosite | Level 12
No, I modified it.

sas-innovate-white.png

Join us for our biggest event of the year!

Four days of inspiring keynotes, product reveals, hands-on learning opportunities, deep-dive demos, and peer-led breakouts. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

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
  • 9 replies
  • 1794 views
  • 5 likes
  • 5 in conversation