08-27-2015 07:33 PM
Quick question that I can't seem to find an answer to. If I am substringing the year off of the MHDIAGYR_P variable like so:
diagyrch = substr(MHDIAGYR_P,8,4);
how would I then convert this diagyrch character variable to a numeric variable named diagyrnum?
I am trying something like this....
However, I can't figure out what the correct informat to use is in my input function.
Here is an example value of MHDIAGYR_P: UN-UNK-1984
so diagyrch = 1984 but I want SAS to read that as the Orwellian nineteen eighty-four.
Any help would be greatly appreciated!
08-28-2015 03:27 AM
08-27-2015 08:56 PM
I'm not sure you gain any value in having a SAS date versus a 4 digit number.
If you can't find an information consider using 4. to convert to numeric and then MDY to create a date.
08-28-2015 07:08 PM
The reason I wanted a date format is because if I want to create a listing that has dates, and some have fully known dates, but some have UN-UNK-YYYY, and I want to use the same variable for both instances, where the variable I want to display is already a date format.
|if CMSTDAT = . & substr(CMSTDAT_P,1,1)='U' then CMSTDAT = input(substr(CMSTDAT_P,8,4),4.);|
The variable I want to use on my listing is CMSTDAT; however, the above code doesn't work because when I try to change the value of CMSTDAT (which is a blank numeric value, but CMSTDAT_P is a populated character value) it interprets the date wrong. Since SAS stores dates as the number of seconds away from Jan 1st 1960, could I convert the year value created from the input& substr functions by multiplying it out? If so how would I do that?
Thank you for your reply!
08-28-2015 09:15 PM
You can't change a variable type in place.
But you can create a new variable, but you have to assign a day/month to the year as well.
if substr(CMSTDAT_P,1,1)='U' then CMSTDAT_DATE = mdy(1,1, input(substr(CMSTDAT_P,8,4),4.));
else CMSTDAT_DATE=input(cmstdat, date9.);
You might need to play around with that a bit as I'm not 100% sure what your data looks like. You may be better off posting a new question with what your data looks like and what you want.
08-28-2015 02:50 AM
Easiest way :
= substr(..) + 0;
Real programmers don't like the notes it generates, but to me it doesn't matter.
This "trick" also works in Excel :-)
Another option : start with defining the diagyrch variable as numeric using a length statement.
length diagyrch 8;
08-28-2015 08:05 AM
It WILL matter to you, once you've been bitten in the ass by the consequences of an implicit type cast.
Any NOTE that goes beyond "dataset X created, with Y records and Z colums" raises a red flag with me.
08-28-2015 03:27 AM
Need further help from the community? Please ask a new question.