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

Hello all,

 

I have below dataset

subjid col1 col2 col3 col4 col5 col6 col7 col8
1 A A B B B B C  
2 A A B B C A B  
3 A B A B B A C C
4 G D F F   K    

 

I need to populate for subjects if any of the values from col1-col8 are "A" or "B" or "C" as valid ,if not as invalid

In the above dataset, subjects ,1,2 & 3 are valid and 4 is invalid  as per the above condition.

 

I have tried the below code , but it's not giving desired result

 

data test;
set dsn;
array num(*) $ COL1_COL8;
do i= 1 to dim(ses);
if num(i) in ("A","B","C") then t="Valid";
else t="Invalid";
end;
run;

 

Appreciate your response!

 

Regards

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Remove the ELSE.

Initialize the condition to FALSE and in the DO loop set it TRUE when the condition is met.

data test;
  set dsn;
  array num COL1-COL8;
  t='Invalid';
  do index= 1 to dim(num) while (t='Invalid');
    if num[index] in ('A','B','C') then t='Valid';
  end;
  drop index;
run;

The meaning of the code is even clearer if you create a BOOLEAN numeric variable instead of a character variable.

data test;
  set dsn;
  array num COL1-COL8;
  do index= 1 to dim(num) until (VALID);
    VALID = ( num[index] in ('A','B','C')  );
  end;
  drop index;
run;

 

View solution in original post

1 REPLY 1
Tom
Super User Tom
Super User

Remove the ELSE.

Initialize the condition to FALSE and in the DO loop set it TRUE when the condition is met.

data test;
  set dsn;
  array num COL1-COL8;
  t='Invalid';
  do index= 1 to dim(num) while (t='Invalid');
    if num[index] in ('A','B','C') then t='Valid';
  end;
  drop index;
run;

The meaning of the code is even clearer if you create a BOOLEAN numeric variable instead of a character variable.

data test;
  set dsn;
  array num COL1-COL8;
  do index= 1 to dim(num) until (VALID);
    VALID = ( num[index] in ('A','B','C')  );
  end;
  drop index;
run;

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 1 reply
  • 451 views
  • 1 like
  • 2 in conversation