BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
tkallday33
Calcite | Level 5

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

1 ACCEPTED SOLUTION
7 REPLIES 7
Reeza
Super User

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.

tkallday33
Calcite | Level 5

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!

Reeza
Super User

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.


EH
Obsidian | Level 7 EH
Obsidian | Level 7

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

Kurt_Bremser
Super User

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.

Reeza
Super User

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

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 4944 views
  • 9 likes
  • 4 in conversation