【问题标题】:SAS output statementSAS 输出语句
【发布时间】:2015-03-30 20:55:24
【问题描述】:

我正在尝试更改原始数据文件的输出。在原始数据中,数据格式如下:

Name    Test1   Test2   Test3   Test4
xyz     45      78      88      100
avb     -1      89      76      29

但是我想把数据结构改成如下格式:

Name    Score
xyz     45
xyz     78  
xyz     88
xyz     100
xyz     89 (skip -1 because it's less than 0) 

我正在尝试使用数组和输出语句,但遇到了问题。这是我的代码:

Data Stats; 
  set Record; 
  Array Test(*) Test1 - Test 6; 
  do i = 1 to 6; 
    if Test(i) gt -1 then output; 
  end;
run;

【问题讨论】:

  • 请提供您尝试过的代码,以便我们提供有用的反馈。
  • 我使用的代码是Data Stats;设置记录;数组测试(*)测试1 - 测试6;做 i = 1 到 6;如果 Test(i) gt -1 则输出;结尾;谢谢
  • 请在以后将其编辑到问题中。

标签: sas


【解决方案1】:

您缺少的是将值移动到单个列中。现在你得到了正确的行数,但你没有得到单列。

Data Stats; 
  set Record; 
  Array Test(*) Test1 - Test 6; 
  do i = 1 to 6; 
    if Test(i) gt -1 then do;
      score=test(i);  *save value onto Score variable;
      output; 
    end;
  end;
  keep score name;  *only keep the two variables you want;
run;

【讨论】:

    【解决方案2】:

    您可以使用 proc transpose 或 Array 语句与 do 循环来解决您的问题。

    data record;
    infile datalines missover;
    input Name  $ Test1   Test2   Test3   Test4;
    datalines;
    xyz     45      78      88      100
    avb     -1      89      76      29
    ;;;;
    run;
    
    /* Solution #1 : Using PROC TRANSPOSE */
    proc transpose data=record out=solution_1(rename=(col1=score) where=(score>0) drop=_name_);
    by notsorted name;
    var test1--test4;
    run;
    proc print data=solution_1;run;
    
    
    /* Solution # 2 : Using Array and do loop */
    data solution_2;
    set record;
    array temp(*) test1-test4;
    
    do i=1 to dim(temp);
    score=temp[i];
    if score >0 then output;
    end;
    keep name score;
    run;
    proc print data=solution_2;run;
    

    【讨论】:

      【解决方案3】:

      你可以用非常简单的方法解决这个问题。

          data original;
      
          input Name $ Test1-Test4;
      
          cards;
      
          xyz 45 78 88 100
      
          avb -1 89 76 29
          ;
      
          run;
      
      
      
          ***now you need to sort your data by Name *************;
      
          proc sort data=original;
          by Name;
          run;
      
      ********************************************************;
      
          data u_want;
      
          set original;
      
          array Test[4];
      
          do i=1 to dim(Test);
      
          if Test[i] >= 0 then Score=Test[i]; output;
      
          end;
      
          Drop i Test1-Test4;
      
          run;
      
          **********************************************************;
      
          proc print data=u_want;
      
          run;
      
      Thanks!
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-10-05
        • 1970-01-01
        • 2023-03-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多