jji87
jji87

Reputation: 23

SAS Assign variable if it meets any of multiple string requirements

if find(upcase(owner,in('ADMIN'|'ASSOC'|'BANK'|'CHRIST'|'CHURCH'|'CITY'|)))
THEN NameFlag = 1;
ELSE NameFlag = 0;
IF NameFlag > 0 then NameExclusion=1;
else NameExclusion=0;

I want this if statement to search variable OWNER for any string featured in the in statement and assign a flag if it hits. Ideally I'd like to point this string at a separate data-set that I can just update on the fly, but I'm not quite sure I know how to do that yet. Any help would be appreciated.

Thanks!

Upvotes: 1

Views: 1362

Answers (3)

Longfish
Longfish

Reputation: 7602

If I understand your question correctly, the values you are searching for are substrings in the Owner variable, rather than the whole value. If that's the case then you can use the regular expression function prxmatch to find the strings. This enables multiple searches against a variable, something which you can't do with the find function.

I've started by assigning the search values to a macro variable, for later use. I don't know why you have the NameExclusion variable in your code, as it takes exactly the same value as NameFlag

%let search_str = ADMIN|ASSOC|BANK|CHRIST|CHURCH|CITY;

data have;
input owner & $50.;
NameFlag = prxmatch("/\b(&search_str.)\b/",owner)>0; /*sets value to 1 if true, 0 if false */
datalines;
ADMIN CLERK
TEST VALUE
SOME BANK
THE CHURCH
CHURCHILL
INTERCITY
;

Upvotes: 2

DomPazz
DomPazz

Reputation: 12465

To make this dynamic, use a data set with the values.

data values;
informat value $24.;
input value $;
datalines;
ADMIN
ASSOC
BANK
CHRIST
CHURCH
CITY
;
run;

%put &values;

proc sql noprint;
select "'" || strip(value) || "'"
into :values separated by ','
from values;
quit;

data want;
set have;
If upcase(owner) in (&values)
 Then NameFlag=1;
Else NameFlag=0;
run;

Upvotes: 1

Jay Corbett
Jay Corbett

Reputation: 28391

Try this

If upcase(owner) in ('ADMIN', 'ASSOC', 'BANK', 'CHRIST', 'CHURCH', 'CITY')
 Then NameFlag=1;
Else NameFlag=0;

Upvotes: 2

Related Questions