Informat to only read in a year?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 9
Accepted Solution

Informat to only read in a year?

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
Solution
‎08-28-2015 03:27 AM
Super User
Posts: 7,757

Re: Informat to only read in a year?

Posted in reply to tkallday33

diagyrnum = input(substr(MHDIAGYR_P,8,4),4.);

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Super User
Posts: 19,768

Re: Informat to only read in a year?

Posted in reply to tkallday33

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.

Occasional Contributor
Posts: 9

Re: Informat to only read in a year?

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!

Super User
Posts: 19,768

Re: Informat to only read in a year?

Posted in reply to tkallday33

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.


Contributor EH
Contributor
Posts: 32

Re: Informat to only read in a year?

Posted in reply to tkallday33

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

Super User
Posts: 7,757

Re: Informat to only read in a year?

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 19,768

Re: Informat to only read in a year?

When doing a code review you have to check why all calculations generate errors/notes. This just adds to the list...

Solution
‎08-28-2015 03:27 AM
Super User
Posts: 7,757

Re: Informat to only read in a year?

Posted in reply to tkallday33

diagyrnum = input(substr(MHDIAGYR_P,8,4),4.);

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 455 views
  • 8 likes
  • 4 in conversation