Soham Sinha
Soham Sinha

Reputation: 21

How do I use loop in the following problem?

I want to loop through some variables and create nine new variables which are equal to 1 if the first three variables are "433" or "434". I am using the following code but its showing 1 for everything, can someone help

data icd_dim;
set asg3;
array icd (9) $ ICD9_DGNS_CD_1-ICD9_DGNS_CD_9;
array flag_icd (9) flag_icd_1-flag_icd_9;
do i = 1 to 9;
if icd(i)=substr(icd(i),1,3)="433" OR "434" then flag_icd(i)=1; end;
run;

Upvotes: 2

Views: 54

Answers (2)

Richard
Richard

Reputation: 27526

The in: operator is a prefix comparison operator for character values. You also might want a flag variable name that is more descriptive of the diagnosis.

occlusion_flag(i) = icd(i) in: ('433', '434');

Upvotes: 1

Tom
Tom

Reputation: 51621

You are looking for the IN operator to compare one value to a list of value. Your current code is testing if "433" is True or False. Since it is non-blank it is treated as True.

You want:

if substr(icd(i),1,3) in ("433" "434") then flag_icd(i)=1; 

Or to eliminate the missing values just assign the result of the test to the variable and get either 1 (true) or 0 (false).

flag_icd(i) =  substr(icd(i),1,3) in ("433" "434") ;

Upvotes: 1

Related Questions