BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
smackerz1988
Pyrite | Level 9

Hello, This is a follow up from a previous post

 

https://communities.sas.com/t5/SAS-Programming/Populating-variable-based-on-matching-phrases/m-p/833...

 

This is the code I wrote based off the response

 

data have;
input USUBJID $ LBSTNRC :$40. LBNRIND :$14. AVALC :$200.;
infile datalines dlm = '|';
datalines;
1001|Negative|NORMAL|Negative        
1001|5.0, 6.0, 7.0, 8.0|NORMAL|6.0
1001|6.0, 7.0, 8.0, 9.0|NORMAL|7.0
1001|5.0, 6.0, 7.0, 8.0|NORMAL|5.0
1001|Negative|ABNORMAL|5
1001|None, Occasional, 1-2|NORMAL|Occasional
1001|None, Occasional, 1-5|NORMAL|None                  
;

* ANRIND *;
if lbstrnc ne '' then do;
 if findw(lbstnrc,strip(avalc),', ')>0 then anrind ='Normal';
end;
if missing(lbstnrc) then anrind = propcase(lbnrind,'/');

However, I misinterpreted the spec requirements and it should be set to Normal for results that have comma separated values only. Also, the code provided does not work if the value is the first value in the LBSTNC e.g. row 4 or row 7 in data provided?. How can I amend this?

1 ACCEPTED SOLUTION

Accepted Solutions
smackerz1988
Pyrite | Level 9

Sorry yes I guess I wasn't clear enough but after working through it this is the solution I wanted

 

data have;
input USUBJID $ LBSTNRC :$40. LBNRIND :$14. AVALC :$200.;
infile datalines dlm = '|';
datalines;
1001|Negative|NORMAL|Negative        
1001|5.0, 6.0, 7.0, 8.0|NORMAL|6.0
1001|6.0, 7.0, 8.0, 9.0|NORMAL|7.0
1001|5.0, 6.0, 7.0, 8.0|NORMAL|5.0
1001|Negative|ABNORMAL|5
1001|None, Occasional, 1-2|NORMAL|Occasional
1001|None, Occasional, 1-5|NORMAL|None                  
;

data want;
length anrind $20.;
set have;

 * ANRIND *;
if lbstnrc ne '' and index(lbstnrc,',')>0 then do;
 if findw(lbstnrc,strip(avalc),', ',' ,')>0 then anrind ='Normal';
end;
else anrind ='Abnormal';
run;

 

 

View solution in original post

2 REPLIES 2
ballardw
Super User

Lets address the second bit first:
"Also, the code provided does not work if the value is the first value in the LBSTNC e.g. row 4 or row 7 in data provided?. How can I amend this?: (I have to assume from your example that you actually meant "row 5 or row 7" of this doesn't make any sense at all as Row 4 has comma values.

The data was read into the incorrect variables. So there is a problem(or more than one) in the preceding steps reading the data. Personally I would say that until that is addressed you are wasting time as if this happens for one variable it may have happened for more and any code based on that set is questionable.

 

Second: "should be set to Normal for results that have comma separated values only." Are there any restrictions on what should be separated by the commas"?

Test for presence of a comma: index(lbstnrc,',') > 0. If there are no commas, or the variable is blank then the result is 0.

But now you may have an issue with not describing what to do with exactly one value such as "6.0".

 

You really need to show examples of the desired output as your "spec" language is pretty vague and I can't tell if you are adding on, replacing or supplementing only portions of the discussion from the previous post.

 

smackerz1988
Pyrite | Level 9

Sorry yes I guess I wasn't clear enough but after working through it this is the solution I wanted

 

data have;
input USUBJID $ LBSTNRC :$40. LBNRIND :$14. AVALC :$200.;
infile datalines dlm = '|';
datalines;
1001|Negative|NORMAL|Negative        
1001|5.0, 6.0, 7.0, 8.0|NORMAL|6.0
1001|6.0, 7.0, 8.0, 9.0|NORMAL|7.0
1001|5.0, 6.0, 7.0, 8.0|NORMAL|5.0
1001|Negative|ABNORMAL|5
1001|None, Occasional, 1-2|NORMAL|Occasional
1001|None, Occasional, 1-5|NORMAL|None                  
;

data want;
length anrind $20.;
set have;

 * ANRIND *;
if lbstnrc ne '' and index(lbstnrc,',')>0 then do;
 if findw(lbstnrc,strip(avalc),', ',' ,')>0 then anrind ='Normal';
end;
else anrind ='Abnormal';
run;

 

 

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
  • 2 replies
  • 787 views
  • 0 likes
  • 2 in conversation