如何在SAS中创建一个水平表,其中数据值(非变量)作为SAS中的行项

时间:2016-10-30 01:55:04

标签: sas

使用下面的数据集,我试图创建一个完整的水平表,其中列中的所有7个变量和字符值“完成”的1行显示每个变量的频率。

District      Var1        Var2        Var3            Var4             Var5            Var6        Var7
District1   Complete Partiallycomplete Incomplete   Partiallycomplete Complete        Incomplete    Complete
District2   Partiallycomplete   Incomplete  Complete    Incomplete  Partiallycomplete   Complete    Complete
District3   Incomplete  Complete    Partiallycomplete   Complete    Incomplete  Partiallycomplete   Complete
District4   Complete    Partiallycomplete   Incomplete  Partiallycomplete   Complete    Incomplete  Partiallycomplete
District5   Partiallycomplete   Incomplete  Complete    Incomplete  Partiallycomplete   Complete    Partiallycomplete
District6   Incomplete  Complete    Partiallycomplete   Complete    Incomplete  Partiallycomplete   Partiallycomplete
District7   Complete    Partiallycomplete   Incomplete  Partiallycomplete   Complete    Incomplete  Incomplete
District8   Partiallycomplete   Incomplete  Complete    Incomplete  Partiallycomplete   Complete    Incomplete
District9   Incomplete  Complete    Partiallycomplete   Complete    Incomplete  Partiallycomplete   Incomplete
District10  Complete    Partiallycomplete   Incomplete  Partiallycomplete   Complete    Incomplete  Incomplete

但是,我面临两个问题:

1。)无法将所有7个变量列合并为1个水平表。 SAS为7个变量中的每一个输出7个单独的表。我试图在每个“Proc Freq”程序中包含多个“where”语句作为解决方案失败。

2.。无法将行设为变量值(例如:“完成”)。

我试过的代码:

proc freq data= Mon.Montest11;
Table District*(Var1)/list;
where Var1="Complete";
Run;

/* Above code works fine ONLY for 1 Variable. When I add 6 more variables as shown below, it doesn't work out */

proc freq data= Mon.Montest11;
Tables District*(Var1 Var2 Var3 Var4 Var5 Var6 Var7)/list;
Run;

/* 7 Separate tables generated*/

我想要的是什么:

                                         Var 1  Var 2  Var 3  Var 4
"Complete"                               10    20      30      40
"Partially Complete"                     20    30      40      50
"Incomplete"                             30    40      50      60

我也尝试将字符转换为数字变量以查看是否有帮助,但事实并非如此。这可能是一个简单的初级问题,但我遇到了困难,我将不胜感激!

由于

1 个答案:

答案 0 :(得分:0)

我之前的尝试没有成功,但下面的代码已经过测试,应该会为您提供所需的结果:

data want;
length district $ 20;
format district $20.;
keep district y1-y7;
set have end=last;

array c[7] ;
array p[7] ;
array inc[7] ;
array x[7] $20 var1-var7;
array y[7];
retain c p inc;


if _n_ = 1 then do i=1 to 7;
c[i]=0;
p[i]=0;
inc[i]=0;
end;
do i = 1 to 7;
  if x[i] = 'Complete' then c[i]= c[i]+1;
  if x[i] = 'Incomplete' then inc[i]= inc[i]+1;
  if x[i] = 'Partiallycomplete' then p[i]= p[i]+1;
  put i x[i] c[i] inc[i] p[i];
end;

if last then do;
do i = 1 to 7;
  district = 'Complete';
  y[i] = c[i];

end;
output;

do i = 1 to 7;
  district = 'Partiallycomplete';
  y[i]=p[i];

end;
output;

do i = 1 to 7;
  district = 'Incomplete';
  y[i]=inc[i];

end;
output;
end;
run;