Using SAS 9.4
data raw.breast (where=(DIAG_PRTY_SEQ_NBR =1));
set raw.EDVISITS_BREAST;
IF DIAG_DESC = 'pain' then DIAG_DESC2 = 'Pain';
IF DIAG_DESC = 'nausea' then DIAG_DESC2 = 'Nausea';
run;
I am trying to create a new variable and use a wildcard to capture any instance where the word 'pain' is used and group it together into a single bucket 'Pain' category. My code is creating the new variable but is not pulling anything into that column. Any help would be great. Thank you!
Use the index() function:
IF index(DIAG_DESC,'pain') then DIAG_DESC2 = 'Pain';
Use the index() function:
IF index(DIAG_DESC,'pain') then DIAG_DESC2 = 'Pain';
Will this work if I need to put multiple if then statements?
You need to use custom defined formats if there are too many
It will work, sort of.
First, note that you need to set a length for your new variable. Otherwise, it will be defined as 4 characters long because the first mention of it sets it to "Pain".
Second, how do you propose to handle the situation if your string contains two (or more) of the words you want to search for? You only have one new variable to use, to hold the result.
Third, you probably would want to ignore upper vs. lower case. So this might be a better approach:
length diag_desc2 $ 20;
if index(upcase(diag_desc), 'PAIN') then diag_desc2='Pain';
This will catch variations such as Pain, pain, and PAIN.
I changed the length.
It is a priority number so it should not hit on multiple words.
I did not think about upper and lowercase so that is helpful. Thank you
I would add to Kurt's solution to consider if your source could have both uppercase and lower case versions of 'pain'. If so, I'd wrap that text field with an UPCASE function and then search for 'PAIN' (or vice versa).
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.