Mike L
Mike L

Reputation: 496

Multiple Conditions/variables in an IF-THEN statement SAS

I have a data set which has two variables I'm trying to create new groups from. The first variable is "religiosity" and the second is "Av_Anti", both are numeric variables. I'm trying to create groups to separate into 9 groups, with low/mid/high religiosity AND low/mid/high Av_Anti.

DATA LYING1;
SET LYING;
    IF RELIGIOSITY = (1 OR 2) AND Av_anti <=3 THEN Rel_Anti = "LowR,LowA";
    IF RELIGIOSITY = (1 OR 2) AND Av_anti (>3 AND <=7) THEN Rel_Anti = "LowR,MidA";
    IF RELIGIOSITY = (1 OR 2) AND Av_anti >7 THEN Rel_Anti = "LowR,HighA";
    IF RELIGIOSITY = (3 OR 4 OR 5) AND Av_anti <=3 THEN Rel_Anti = "MidR,LowA";
    IF RELIGIOSITY = (3 OR 4 OR 5) AND Av_anti (>3 AND <=7) THEN Rel_Anti = "MidR,MidA";
    IF RELIGIOSITY = (3 OR 4 OR 5) AND Av_anti >7 THEN Rel_Anti = "MidR,HighA";
    IF RELIGIOSITY = (6 OR 7) AND Av_anti <=3 THEN Rel_Anti = "HighR,LowA";
    IF RELIGIOSITY = (6 OR 7) AND Av_anti (>3 AND <=7) THEN Rel_Anti = "HighR,MidA";
    IF RELIGIOSITY = (6 OR 7) AND Av_anti >7 THEN Rel_Anti = "HighR,HighA";
    ELSE Rel_Anti = "Error";
RUN;

I keep getting the message

  "The function AV_ANTI is unknown, or cannot be accessed." 

The thing is, I've checked that variable, its spelled correctly and its listed in the called dataset. I don't know why SAS wouldn't be recognizing it as a variable? I also seem to be getting some issues with the logical operators (the >, <=). Am I missing something?

Upvotes: 2

Views: 47743

Answers (1)

Reeza
Reeza

Reputation: 21264

You have invalid SAS Syntax in your comparisons/ranges. You should use the IN operator to check for inclusion in a list of values and then compare each variable to boundaries for each comparison. (i.e. not Not:

Av_anti (>3 AND <=7) 

But:

Av_anti>3 and Av_anti<=7

or :

3<AV_atni<=7

The following should work. I also think you intend to have else if rather than all if's throughout, otherwise your last condition will set all to Error or LowR/MidA.

DATA LYING1;
SET LYING;
    IF RELIGIOSITY in (1, 2) AND Av_anti <=3 THEN Rel_Anti = "LowR,LowA";
    ELSE IF RELIGIOSITY in  (1, 2) AND (Av_anti >3 AND AV_anti<=7) THEN Rel_Anti = "LowR,MidA";
    ...
    ELSE Rel_Anti = "Error";
RUN;

A better solution might be:

Data lying1;
    set lying;
    IF RELIGIOSITY in (1, 2) then P1='LowR';
    else IF RELIGIOSITY in (3, 4, 5) then P1='MidR';
    else IF RELIGIOSITY in (6,7) then P1='HighR';
    else P1='Error';

    if av_anti<=3 then P2='LowA';
    else if  3<av_anti<=7 then P2='MidA';
    else if AV_Anti>7 then 'HighA';
    else P2='Error';

    Rel_Anti=catx(",", P1, P2);
run;

Upvotes: 10

Related Questions