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

Dear users,

I just started to use SAS and need some help for an urgent paper. My data set contains an alphanumeric variable where the first position is a character followed by a four digit number. I want to create a new variable which has the value one if the character plus the first three digits of the number match. F.e. the value of the variable M968 should be 1 if the observation contains "M968", so an obeservation with "M9680" would be one and "M9670" would be zero.

My following code only recognizes if the complete value matches:

data multimorbid;

       set library.file;

       M968 = 0;

      if icd_hd = 'M968' then M968=1;

run;

I tried to use the where statement, because I can use the operator contains, but using the where statement creates only a subset of observations which contain the character strings, but I want that the new variable M968 is added to the complete data set multimorbid.

Best,

Dennis

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Try using the operator =: which checks if the variable starts with M968

Or

You can use the substring function:

if substr(icd_hd, 1, 4)="M968" then M968=1;

data multimorbid;

       set library.file;

       M968 = 0;

      if icd_hd =: 'M968' then M968=1;

run;

View solution in original post

1 REPLY 1
Reeza
Super User

Try using the operator =: which checks if the variable starts with M968

Or

You can use the substring function:

if substr(icd_hd, 1, 4)="M968" then M968=1;

data multimorbid;

       set library.file;

       M968 = 0;

      if icd_hd =: 'M968' then M968=1;

run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 694 views
  • 0 likes
  • 2 in conversation