BookmarkSubscribeRSS Feed
ClaireZ
Calcite | Level 5

I am trying to determine the difference between dates for survdate and HIVdate using SAS date values. The format for the variable HIVTSTD2 is MMYYYY and I added the date "15" to all observations so it was in the DDMMYYYY format.  When I run this code below, this is the error statement that I receive:

662  put hivdate= hivdate ddmmyy10.;

                          ---------

                          48

ERROR 48-59: The format $DDMMYY was not found or could not be loaded.

Code:

data work0607; set work06 work07;

survdate=mdy(imonth,iday,iyear);

put survdate= survdate ddmmyy10.;

HIVday=15;

HIVdate=cat(hivday, HIVTSTD2); *merge the date 15 with all MMYYYY observations found in HIVTSTD2;

put hivdate= hivdate ddmmyy10.;

if hivdate=15 then hivdate=.;

duration = YRDIF(hivdate,survdate,'actual');

run;

I appreciate any assistance with this.

-Claire

6 REPLIES 6
Tom
Super User Tom
Super User

You cannot apply a numeric format (all date formats are ways to format numbers to look like dates) to the character variable you created by concatenation.

Anotherdream
Quartz | Level 8

To elaborate more on what Tom has said (although he said it very distinctly, and he is 100% correct).

Sas dates are stored as numbers, the number of days between 1-1-1960 and today to be exact. When you format the date, all you are doing is making the number display as the date format, however the base data is still a number. Example: the Date 10/11/1987 is actually stored as the number 10145.

You are having problems because you are using the concatenation function, and this function makes your new variable HIVdate into a Character variable.

Since Date formats only apply to Numbers, sas cannot apply your format. You could first alter your character variable into a number using an input statement similar to below, and then format it as a date if you are interested.

HIVNum=input(HIVdate,32.);

Note: Your code above will take the Number of days between 1-1-1960, in number form (so 10145 for 10/11/1987) and then it will add 15 to the front of it (giving you 1510145, or a sas date of 08/21/6094).  Is this what you were intending to do?

Based upon your original comment, It looks to me like you need to define your HIVDate using the MDY function, specifying your value of 15 as your DAY. If you need I can give you exact code to do this.


ClaireZ
Calcite | Level 5

Thanks Anotherdream for your thorough response. Now it makes sense.

As for your final note in your response, I am trying to define the date as 15, however my variable is in the MMYYYY format (11998 for Jan 1998 or 102004 for Oct 2004). If you wouldn't mind, it would really be helpful to see what the code looks like to restructure this variable to be MM15YYYY. Also, since the month number for my MMYYYY variable is entered as one digit for months 1-9 and obviously two digits for 10-12, will I need to include a leading zero in order for SAS to interpret the months 1-9 correctly?

Thank you so much from your humble SASer,

Claire

ClaireZ
Calcite | Level 5

Thanks, Tom!

Anotherdream
Quartz | Level 8

Hello ClaireZ, I would not mind helping at all. I do have a quick question for you however, as this answer will determine which way the code goes.

You say your date variable is currently formatted as MMYYYY. When you say this, do you mean that the date value is a number  input with the actual format MMYYYY (as this is a valid format), or is it a character variable that just happens to say "11998"?

Basically the method is different depending on which way your data is stored. If it is a charcter variable you would do something like below

data NEWDATA;

set OLDDATA;

newvariable=mdy(reverse(substr(reverse(compress(Oldvar)),5,2)),15,reverse(substr(reverse(compress(Oldvar)),1,4));

format newvariable mmddyy10.;

run;

/*Please note this code is not tested, only assumed. Basically what it is doing is scanning the word from right to left for the first 4 values (aka your year), and then scanning the word for the 5th and 6th observation (from the right side, or the 1-2 from the left side).  It is then putting these values into the Month, Day, year funciton.

If it is stored as an actual number already, I would need to know what day of the month is stored. (Example, if you had the date 3/18/2012 and formatted it as MMYYYY, you would get 03-2012, however the 18th day is still part of the actual date, you are simply not showing it).

However for general terms, you could always do this...

Data NEWDATA;

set OLDDATA:

newvariable=intnx('month',OLDVARIABLE,0) + 15;

format newvariable mmddyy10.;

run;

/* Again un-tested but basically what the code is doing is converting the number month to the first of the month, and then adding 15 days to it, giving you the date format that you want*/

Please let me know if that didn't help, as when I have more time tonight I can sit down and test out the code to give you a definitive working answer!


ClaireZ
Calcite | Level 5

Hallelujah! The code for a character variable worked. Thank you so so much.

-Claire

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

New Learning Events in April

 

Join us for two new fee-based courses: Administrative Healthcare Data and SAS via Live Web Monday-Thursday, April 24-27 from 1:00 to 4:30 PM ET each day. And Administrative Healthcare Data and SAS: Hands-On Programming Workshop via Live Web on Friday, April 28 from 9:00 AM to 5:00 PM ET.

LEARN MORE

Discussion stats
  • 6 replies
  • 2514 views
  • 0 likes
  • 3 in conversation