楼主: lovelybetty1208
31616 15

sas 分类求和、求占比的 问题 [推广有奖]

11
lovelybetty1208 发表于 2013-4-3 20:39:59
北京客户数据


client        city
a001        1
a002        1
a003        1
a004        1
b004        1
b003        1
b002        1
b001        1
c001        1
e001        1
f011        1
w011        1
d009        1
d008        1
耶和华是我的牧者,我必不致缺乏。
他使我躺卧在青草地上,
领我在可安歇的水边。

12
lovelybetty1208 发表于 2013-4-4 20:50:22
ding
耶和华是我的牧者,我必不致缺乏。
他使我躺卧在青草地上,
领我在可安歇的水边。

13
lovelybetty1208 发表于 2013-4-4 21:02:47
ding
耶和华是我的牧者,我必不致缺乏。
他使我躺卧在青草地上,
领我在可安歇的水边。

14
yongyitian 发表于 2013-4-4 21:27:01
没弄明白第二个表第六列是什么意思,可能不是LZ想要的.

是不是这样

data testsale;
infile datalines dlm=',';
input sales $ client $ type times amount;
datalines;
jame,a001,1,10,45
jame,a001,0,3,.
jame,a002,1,11,67
jame,a002,0,9,.
jame,a003,1,33,99
jame,a003,0,10,.
jame,a004,0,34,.
david,a002,1,11,22
david,a002,0,23,.
david,b001,1,12,27
david,b001,0,16,.
david,b002,1,13,38
david,b002,0,9,.
david,b004,1,14,38
david,b004,0,43,.
david,c001,1,15,38
micheal,a001,1,25,26
micheal,a001,0,16,.
micheal,a002,1,16,49
micheal,a002,0,17,.
micheal,b003,1,18,77
micheal,b003,0,19,.
micheal,d001,1,16,58
micheal,d001,0,21,.
micheal,e001,0,22,.
jacky,f011,1,26,56
jacky,f011,0,21,.
jacky,d009,1,13,59
jacky,d009,0,16,.
jacky,d008,1,17,94
jacky,d008,0,17,.
jacky,w011,1,17,83
jacky,w011,0,33,.
jacky,q001,1,32,59
jacky,q001,0,25,.
jacky,a002,1,27,126
jacky,a002,0,37,.
jacky,c012,1,18,117
jacky,c012,0,9,.
jacky,b003,1,42,231
jacky,b003,0,33,.
; run;

data city;
input client $ city;
datalines;
a001  1
a002  1
a003  1
a004  1
b004  1
b003  1
b002  1
b001  1
c001  1
e001  1
f011  1
w011  1
d009  1
d008  1
;

proc sql;
create table sale_city as
    select *
    from testsale a left join city b
    on a.client=b.client;
quit;

proc sql;
    create table table1 as
    select sales,
           count(distinct client) as NB_Client,
           sum(times)  as TB_sale_time,
           sum(amount) as TB_amount,
          (calculated NB_Client )/(select count(distinct Client) from sale_city)  
                       as Percent_B_Client     format=percent6.2,
          (calculated TB_sale_time)/(select sum(times) from sale_city where type=1)
                       as Percent_B_sale_time  format=percent6.2,
          (calculated TB_amount)/ (select sum(amount) from sale_city where type=1)
                       as Percent_B_amount     format=percent6.2
    from sale_city   
        where type=1 and city=1
        group by sales;
quit;

proc sql;
    create table table20 as
    select *,  sum(amount) as TS_amount
    from sale_city  
        group by sales
        having type = 1;

    create table table2 as
      select sales, client,  
         sum(times)  as TSC_time,
         sum(amount) as TSC_amount,
         sum(amount) / TS_amount as Percent_SC  format=percent6.2,
         sum(amount) / (select sum(amount) from table20)
                        as Percent_T_amount     format=percent6.2
    from table20
        group by sales, client;  
    drop table table20;
quit;

proc print data=table1; title 'table1'; run;
proc print data=table2; title 'table2'; run;

15
zkymath 在职认证  发表于 2013-4-4 22:58:17
你把数据搞到一个文本文件当中,以附件形式上传。上面的也成

16
lovelybetty1208 发表于 2013-4-6 23:50:43
yongyitian 发表于 2013-4-4 21:27
没弄明白第二个表第六列是什么意思,可能不是LZ想要的.

是不是这样
感谢你的热心回复。 不过我发现结果和我预想的不太一样。应该是在统计中忽视了没有金额产生的销售行为。其实这也是要算的。


应该得到的结果如下:  该怎样修改代码呢。 我自己试着改了改你的代码,都不理想。

table1:
sales        nb        nb%        freq        freq%        amount        amount%
david        5        100.0%        156        100.0%        163        100.0%
jacky        6        75.0%        297        77.5%        649        78.7%
jame        4        100.0%        110        100.0%        211        100.0%
micheal        4        80.0%        133        78.2%        152        72.4%


table2:
sales        client        times        amount        times%        amount%
david        a002        34        22        21.8%        13.5%
david        b001        28        27        17.9%        16.6%
david        b002        22        38        14.1%        23.3%
david        b004        57        38        36.5%        23.3%
david        c001        15        38        9.6%        23.3%
jacky        a002        64        126        16.7%        15.3%
jacky        b003        75        231        19.6%        28.0%
jacky        c012        27        117        7.0%        14.2%
jacky        d008        34        94        8.9%        11.4%
jacky        d009        29        59        7.6%        7.2%
jacky        f011        47        56        12.3%        6.8%
jacky        q001        57        59        14.9%        7.2%
jacky        w011        50        83        13.1%        10.1%
jame        a001        13        45        11.8%        21.3%
jame        a002        20        67        18.2%        31.8%
jame        a003        43        99        39.1%        46.9%
jame        a004        34        0        30.9%        0.0%
micheal        a001        41        26        24.1%        12.4%
micheal        a002        33        49        19.4%        23.3%
micheal        b003        37        77        21.8%        36.7%
micheal        d001        37        58        21.8%        27.6%
micheal        e001        22        0        12.9%        0.0%
耶和华是我的牧者,我必不致缺乏。
他使我躺卧在青草地上,
领我在可安歇的水边。

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-31 00:15