楼主: FREETREE1208
6313 11

[原创博文] 如何count categorical variable? [推广有奖]

  • 0关注
  • 0粉丝

小学生

14%

还不是VIP/贵宾

-

威望
0
论坛币
10 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
626 点
帖子
3
精华
0
在线时间
6 小时
注册时间
2009-9-3
最后登录
2015-5-28

楼主
FREETREE1208 发表于 2010-5-1 11:08:22 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
有一个数据是这样的,每个entry是顾客的fund account,种类有equity, bonds 和 others



customer     fund category            balances
1                         equity                        100
1                         equity                        200
1                         bonds                         120
2                        bonds                           300
2                         others                        210
3                         bonds                          100


现在我需要把这样的一个数据合并成一个以每个顾客编号为准的文件,里面包括每个fund的数量和总的fund数量
也就是

customer      N_equity        N_bonds    N_others        N_total        total_balance
1                      2                      1                 0                      3
2                      0                        1                1                     2  
3                      0                     1                   0                       1


请教大牛,如何让sas这样count 出这些categorical variable的个数呢?
多谢
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Categorical Variable Count ABLE ABL category customer account equity others

回帖推荐

sushe1527 发表于8楼  查看完整内容

这个是真的么?版主你可要说话算数哈 下面改进了一点,解决了sum() 中的变量太多不方便一一手动输入的问题 N_total=sum(N_bonds,N_others,N_equity); ------------------------------------------------------------------------------- data a; input customer fund$ balances@; cards; 1 equity 100 1 equity 200 1 bonds 120 2 bonds 300 2 others 210 3 bonds 100 ; run; proc ...

本帖被以下文库推荐

沙发
crackman 发表于 2010-5-1 12:14:16
data a;
input customer fund$ balances@;
cards;
1   equity  100
1   equity   200
1   bonds    120
2    bonds  300
2    others 210
3    bonds     100
;
run;
proc sql;
create table b as select customer,fund, count(fund) as count,sum(balances) as  total from a group by customer,fund;
create table b as select customer,fund ,count,sum(total) as total_balances from b group by customer;
quit;
proc transpose data=b out=c prefix=N_;
id fund;
var count;
by customer total_balances;
run;
data d;
set c;
drop _name_;
N_total=sum(N_bonds,N_others,N_equity);
run;

方法很笨
但是可以解决问题

藤椅
sushe1527 发表于 2010-5-1 12:17:36
crackman 发表于 2010-5-1 12:14
data a;
input customer fund$ balances@;
cards;
1   equity  100
1   equity   200
1   bonds    120
2    bonds  300
2    others 210
3    bonds     100
;
run;
proc sql;
create table b as select customer,fund, count(fund) as count,sum(balances) as  total from a group by customer,fund;
create table b as select customer,fund ,count,sum(total) as total_balances from b group by customer;
quit;
proc transpose data=b out=c prefix=N_;
id fund;
var count;
by customer total_balances;
run;
data d;
set c;
drop _name_;
N_total=n(N_bonds,N_others,N_equity);
run;

方法很笨
但是可以解决问题
N_total=n(N_bonds,N_others,N_equity); 这里n是个数,应该是 sum吧?

板凳
crackman 发表于 2010-5-1 12:21:22
就是就是
怎么我一出来就看到你呢
哥们

报纸
sushe1527 发表于 2010-5-1 12:32:18
crackman 发表于 2010-5-1 12:21
就是就是
怎么我一出来就看到你呢
哥们
你抢了我的transpose不找你找谁?

地板
crackman 发表于 2010-5-1 12:46:45
不好意思
你还一个方法
做出来了我给你100论坛币

7
crackman 发表于 2010-5-1 12:50:19
哥们

8
sushe1527 发表于 2010-5-1 17:12:49
crackman 发表于 2010-5-1 12:46
不好意思
你还一个方法
做出来了我给你100论坛币
这个是真的么?版主你可要说话算数哈
下面改进了一点,解决了sum() 中的变量太多不方便一一手动输入的问题
N_total=sum(N_bonds,N_others,N_equity);

-------------------------------------------------------------------------------

data a;
input customer fund$ balances@;
cards;
1   equity  100
1   equity   200
1   bonds    120
2    bonds  300
2    others 210
3    bonds     100
;
run;
proc sql noprint;
select count(distinct ( fund)) into: count from a;
select distinct fund into: fund from a;
create table tt as select distinct (fund) from a;quit;
%macro s;                             
data _null_;                              
set tt;                                 
call symput ('n',_n_);                    
call symput (compress('f'||_n_),fund);
run;  
%mend;
%s;
%macro k;
%DO i = 1 %TO &count;
data j&i;
set a;if fund="&&f&i"  
then n_&&f&i=1 ;
%end;
run;
data w;
merge %do i=1 %to &count;
j&i %end;;
run;
%do i=1 %to &count;
proc sql;create table tmp&i as
select customer ,sum(balances) as  total_balances,
sum(n_&&f&i) as n_&&f&i  from w group customer;quit; %end;
data w2;
merge %do i=1 %to &count;tmp&i %end;;
run;
data ww;set w2;run;
data _null_;
if 0 then set w2;
array _allvar_(*) _all_;
call execute('proc datasets; modify w2;rename ') ;
do i=3 to dim(_allvar_);
rename=catt(vname(_allvar_(i)),'=col',i);
call execute(rename) ;
end;
call execute(';run;quit;') ;
stop;
run;
%mend;
%k;
%let t=%eval(&count+2);
%put &t;
data w3;
set w2;
n_total=sum(of col3-col&t.);
keep customer n_total;
run;
data final;
merge ww w3;
run;
已有 1 人评分经验 论坛币 学术水平 热心指数 收起 理由
crackman + 100 + 100 + 1 + 1 哥们 好复杂啊

总评分: 经验 + 100  论坛币 + 100  学术水平 + 1  热心指数 + 1   查看全部评分

9
sushe1527 发表于 2010-5-1 21:17:55
复杂什么的不是问题 钱拿来~~

10
crackman 发表于 2010-5-1 21:45:28
钱给你了啊

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-18 14:44