Ii Oo
Ii Oo

Reputation: 95

Conditional logic for all values with a prefix

Suppose I have a list of variables with a common prefix. Is it possible to use If/then logic on all of those variables at once?

Data I have 3 columns:

data have;
  length column1-column2 $8 column3 8;
  input column1-column3;
  datalines;
CC A_var1 12
HH A_var2 212
CC A_var3 221
CC B_var1 66
HH B_var2 545
CC B_var3 454
;
* untested;
data  test;
  set source;
  if column1 = 'CC' then A: = .
run;

Upvotes: 1

Views: 563

Answers (1)

Tom
Tom

Reputation: 51566

You can use the : modifier on the operator in the IF statement to do a truncated string comparison.

data want;
  set have;
  if column1='CC' and column2 =: 'A_' then column3=.;
run;

Result

Obs    column1    column2    column3

 1       CC       A_var1         .
 2       HH       A_var2       212
 3       CC       A_var3         .
 4       CC       B_var1        66
 5       HH       B_var2       545
 6       CC       B_var3       454

Upvotes: 1

Related Questions