IF-THEN语句SAS中的多个条件/变量

时间:2014-12-07 20:21:48

标签: if-statement sas

我有一个数据集,它有两个变量,我试图从中创建新的组。第一个变量是" religiosity"第二个是" Av_Anti",两者都是数字变量。我试图创建组分为9组,低/中/高宗教信仰和低/中/高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;

我一直收到消息

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

问题是,我已检查过该变量,其拼写正确且在被调用的数据集中列出。我不知道为什么SAS不会将其识别为变量?我似乎也遇到了逻辑运算符的一些问题(&gt;,&lt; =)。我错过了什么吗?

1 个答案:

答案 0 :(得分:7)

您的比较/范围中包含无效的SAS语法。 您应该使用IN运算符检查是否包含在值列表中,然后将每个变量与每个比较的边界进行比较。 (即不是 不是:

Av_anti (>3 AND <=7) 

但是:

Av_anti>3 and Av_anti<=7

或:

3<AV_atni<=7

以下内容应该有效。我也认为如果不是全部,那么你打算拥有其他的,否则你的最后一个条件将全部设置为Error或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;

更好的解决方案可能是:

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;