sas中的分组方式

sas中的分组方式,sas,Sas,我将以下数据集作为输入 ID -- 1 2 2 3 4 4 4 5 需要一个新的数据集,如下所示 ID count of ID -- ----------- 1 1 2 2 3 1 4 3 5 1 您能告诉我如何在SAS中使用PROC SQL执行此操作吗?尝试以下操作: proc sql noprint; create table test as select distinct id,

我将以下数据集作为输入

ID  
--  

1  
2  
2  
3  
4  
4  
4  
5  
需要一个新的数据集,如下所示

ID   count of ID  
--   -----------

1    1  
2    2  
3    1  
4    3  
5    1  
您能告诉我如何在SAS中使用PROC SQL执行此操作吗?

尝试以下操作:

proc sql noprint;
create table test as select distinct id, count(id)
from your_table
group by ID
order by ID
;
quit;
DATA Have; 
 input id ; 
 datalines;
 1
 2
 2
 3
 4
 4
 4
 5 
 ;

Proc Sort data=Have;
 by ID;
run;

Data Want;
 Set Have;
 By ID;
 If first.ID then Count=0;
 Count+1;
 If Last.ID then Output;
Run;

或者Proc Freq或Proc Summary呢?这样可以避免对数据进行预排序

proc freq data=have noprint;
table id / out=want1 (drop=percent);
run;

proc summary data=have nway;
class id;
output out=want2 (drop=_type_);
run;

您也可以选择只保留新数据集中的Id和N变量。

如果要进行的聚合比较复杂,那么只使用PROC SQL,因为我们更熟悉SQL中的Group by

proc sql ;
create table solution_1 as select distinct ID, count(ID)
from table_1
group by ID
order by ID
;
quit;

  • 如果您使用的是SAS-EG查询生成器,那么它在小型应用程序中非常有用 分析

只需拖放要聚合的列,并在摘要选项中选择要执行的任何操作,如Avg、Count、miss、NMiss等。

我们可以使用simple PROC SQL Count来执行此操作:

 proc sql;
 create table want as
    select id, count(id) as count_of_id 
    from have
    group by id;
 quit;

这是另一种可能性,通常被称为陶氏建筑:

Data want;
  do count=1 by 1 until(last.ID);
    set have;
    by id;
    end;
run;

嗨,有没有可能不使用PROC-SQL呢?只是好奇——为什么不使用SQL呢?嗨,你可以通过使用像Carolina这样的数据步骤来避免SQL。我喜欢SQL的强大功能!非常感谢!!!!!!!!!!!!!!!!!我已经开始学习SAS数据。当然,我的问题非常简单。首先,请深入了解以下问题:
如何在SAS**中不使用PROC SQL执行此操作?**
第二,您复制了第三个答案。
Data want;
  do count=1 by 1 until(last.ID);
    set have;
    by id;
    end;
run;