- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Howdy folks,
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....
diagyrnum=input(diagyrch,date.);
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!
Thanks,
-Tyler
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
diagyrnum = input(substr(MHDIAGYR_P,8,4),4.);
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
Cheers,
Eric
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
When doing a code review you have to check why all calculations generate errors/notes. This just adds to the list...
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
diagyrnum = input(substr(MHDIAGYR_P,8,4),4.);