SAS 中的 Access Crosstab 查询等效吗?

发布于 2024-09-13 18:24:18 字数 365 浏览 9 评论 0原文

这是我的输入:

ID  Color
1   green
1   red
1   orange
1   green
1   red
2   red
2   red
2   blue
3   green
3   red

这是我想要的输出 - 按 ID 为每种颜色列出的记录计数:

ID  green  red  orange blue
1   2      2    1      0
2   0      2    0      1
3   1      1    0      0

我知道我可以使用 proc freq 获取信息,但我想输出一个与我上面编写的数据集完全相同的数据集。我似乎无法弄清楚如何使颜色成为此输出数据集中的列。

Here is my Input:

ID  Color
1   green
1   red
1   orange
1   green
1   red
2   red
2   red
2   blue
3   green
3   red

Here is what I want in my output - a count of records by ID for each color:

ID  green  red  orange blue
1   2      2    1      0
2   0      2    0      1
3   1      1    0      0

I know I can get the information using proc freq, but I want to output a dataset exactly like the one I have written above. I can't seem to figure out how to make the colors the columns in this output dataset.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

戏剧牡丹亭 2024-09-20 18:24:18

首先,生成数据。

data data;
    format ID 8. Color $8.;
    input id color;
datalines;
1   green
1   red
1   orange
1   green
1   red
2   red
2   red
2   blue
3   green
3   red
run;

接下来,按 id 汇总颜色计数。

proc freq data=data noprint;
    table id*color / out=freq;
run;

把桌子弄平。

proc transpose data=freq out=freq_trans(drop=_:);
    id color;
    by id;
    var count;
run;

(可选)用 0 填充缺失的单元格。

data freq_trans_filled;
    set freq_trans;
    array c(*) green red orange blue;
    do i = 1 to dim(c);
        if c[i]=. then c[i]=0;
    end;
    drop i;
run;

first, generate the data.

data data;
    format ID 8. Color $8.;
    input id color;
datalines;
1   green
1   red
1   orange
1   green
1   red
2   red
2   red
2   blue
3   green
3   red
run;

next, summarize color counts by id.

proc freq data=data noprint;
    table id*color / out=freq;
run;

make the table flat.

proc transpose data=freq out=freq_trans(drop=_:);
    id color;
    by id;
    var count;
run;

optionally, fill in missing cells with 0.

data freq_trans_filled;
    set freq_trans;
    array c(*) green red orange blue;
    do i = 1 to dim(c);
        if c[i]=. then c[i]=0;
    end;
    drop i;
run;
负佳期 2024-09-20 18:24:18

您可以使用 PROC FREQTABLE 语句的 SPARSE 选项用零填充缺失的单元格。这样,您就不需要另一个 DATA 步骤。颜色的顺序也可以通过 PROC FREQORDER= 选项来控制。

data one;
  input id color :$8.;
datalines;
1   green
1   red
1   orange
1   green
1   red
2   red
2   red
2   blue
3   green
3   red
run;
proc freq data=one noprint order=data;
  table id*color /out=freq sparse;
run;
proc transpose data=freq out=two(drop=_:);
    id color;
    by id;
    var count;
run;
proc print data=two noobs;
run;
/* on lst
id    green    red    orange    blue
 1      2       2        1        0
 2      0       2        0        1
 3      1       1        0        0
*/

You can fill the missing cells with zero's using the SPARSE option to the PROC FREQ's TABLE statement. This way, you don't need another DATA step. The order of the colors can also be controlled by the ORDER= option to PROC FREQ.

data one;
  input id color :$8.;
datalines;
1   green
1   red
1   orange
1   green
1   red
2   red
2   red
2   blue
3   green
3   red
run;
proc freq data=one noprint order=data;
  table id*color /out=freq sparse;
run;
proc transpose data=freq out=two(drop=_:);
    id color;
    by id;
    var count;
run;
proc print data=two noobs;
run;
/* on lst
id    green    red    orange    blue
 1      2       2        1        0
 2      0       2        0        1
 3      1       1        0        0
*/
一张白纸 2024-09-20 18:24:18

我从来都不喜欢 proc transpose,因为我永远记不住语法。这是一种使用 proc sql 和宏变量来完成此操作的方法。

proc sql noprint;  
    select    sum(color = '" || trim(color) || "') as " || color into: color_list separated by ", "
    from      (select distinct color from one);  
    create table result as  
    select    id,  
              &color_list  
    from      one  
    group by  id;  
quit;



id            blue        green         orange            red  
1              0              2              1              2  
2              1              0              0              2  
3              0              1              0              1  

I've never been a fan of proc transpose because I can never remember the syntax. Here's a way to do it with proc sql and a macro variable.

proc sql noprint;  
    select    sum(color = '" || trim(color) || "') as " || color into: color_list separated by ", "
    from      (select distinct color from one);  
    create table result as  
    select    id,  
              &color_list  
    from      one  
    group by  id;  
quit;



id            blue        green         orange            red  
1              0              2              1              2  
2              1              0              0              2  
3              0              1              0              1  
如果没有 2024-09-20 18:24:18

对于(pteranodon),我碰巧正在审查档案(6年多后),这就是为什么这么不合时宜,但有人可能会受益。

    proc sql noprint feedback;  
     select    catx(' ','sum(color =',quote(trim(color)),') as',color) into: color_list separated by ", "
       from (select distinct color from one);  
     create table result as  
      select id, &color_list  
      from one 
      group by id;  
    quit;

For (pteranodon), I happened to be reviewing the archives(6+ yrs later) which is why so untimely, but someone may benefit.

    proc sql noprint feedback;  
     select    catx(' ','sum(color =',quote(trim(color)),') as',color) into: color_list separated by ", "
       from (select distinct color from one);  
     create table result as  
      select id, &color_list  
      from one 
      group by id;  
    quit;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文