【问题标题】:Count by variables with criteria in SAS按 SAS 中的标准变量计数
【发布时间】:2016-10-26 12:55:38
【问题描述】:

我有以下数据集。我想为每个学生创建一个 Flag(Score>50) 列,以获取分数大于 50 的科目数。 我知道一种方法,方法是为每个主题创建一个使用 if 条件的标志,然后添加它们,但只是好奇在 SAS 中是否有更好的方法来做到这一点?谢谢!!

Student_ID,Physics,Maths,Social,English,Chemistry,Flag(Score>50)
1011,90,90,60,30,20,3
1012,60,90,30,40,40,2
1013,30,10,80,70,50,2
1014,70,10,40,90,90,3

data score1;
set score;
if Physics > 50 then Phy_flag = 1;
if Maths > 50 then Math_flag=1;
if Social > 50 then Social_flag=1;
if English > 50 then Eng_flag=1;
if Chemistry > 50 then Chem_flag=1;
Flag_Score_50 = sum(Phy_flag,Math_flag,Social_flag,Eng_flag,Chem_flag);
run;

这就是我所做的,但我在其他数据集中有太多变量,我不想多次写这些 if 条件。发送

【问题讨论】:

    标签: count sas conditional-statements


    【解决方案1】:

    使用一个数组来保存不同科目的分数,然后遍历数组,计算大于 50 的值。

    data score1;
    set score;
    array subj[5] Physics Maths Social English Chemistry;
    
    Flag_Score_50 = 0;
    do i=1 to 5;
       if subj[i] > 50 then
          Flag_Score_50 = Flag_Score_50 + 1;
    end;
    drop i;
    run;
    

    【讨论】:

      【解决方案2】:

      直接使用sum:

      data have;
      infile cards dlm=',';
      input Student_ID Physics Maths Social English Chemistry;
      flag=sum(Physics > 50,Maths > 50, Social > 50,English > 50,Chemistry > 50);
      cards;
      1011,90,90,60,30,20
      1012,60,90,30,40,40
      1013,30,10,80,70,50
      1014,70,10,40,90,90
      ;
      proc print;
      run;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-01-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多