Proc format issue: working with SAS date values

Reply
New Contributor
Posts: 4

Proc format issue: working with SAS date values

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

Super User
Super User
Posts: 6,849

Re: Proc format issue: working with SAS date values

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.

Super Contributor
Posts: 418

Re: Proc format issue: working with SAS date values

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.


New Contributor
Posts: 4

Re: Proc format issue: working with SAS date values

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

New Contributor
Posts: 4

Re: Proc format issue: working with SAS date values

Thanks, Tom!

Super Contributor
Posts: 418

Re: Proc format issue: working with SAS date values

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!


New Contributor
Posts: 4

Re: Proc format issue: working with SAS date values

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

-Claire

Ask a Question
Discussion stats
  • 6 replies
  • 1654 views
  • 0 likes
  • 3 in conversation