SAS中文论坛

 找回密码
 立即注册

扫一扫,访问微社区

查看: 3514|回复: 22
打印 上一主题 下一主题

sas一个取最大值的问题[求助,急!!!]

[复制链接]

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
楼主
 楼主| 发表于 2010-8-23 10:56:55 | 只看该作者

sas一个取最大值的问题[求助,急!!!]

小弟菜鸟,为了做一个research在硬着头皮学SAS,看了不少书发现也不管用(根本不够用阿。。。)

现在有个棘手的问题,求好心达人帮忙:

有一张表,里面有很多投资公司在不同年份投资了不同行业(总共10个行业),比如甲公司在A行业04年投了2次08年投了3次09年投了1次,D行业08年投了8次,F行业如何如何依次类推等等。
甲公司XX年在A行业投了n次为一个Observation(那些没投资的dummy=0我也都有了)。

现在我要得到这些公司在这些年份之前(比如数据从1980年开始算的是1994年的话就是该公司从1980年到1994年间投资在A/B/C/...行业的数量)所有的投资行业中哪个最多,哪个第二多,并且算出分别投了多少次。
如果这个太麻烦算出当年这个公司投的最多第二多是哪个行业也成(我自己再想办法推到在这年以前的情况)。

我是想要当年这个TOP Industry变成一个新的Variable,然后相应的投资次数再一个新的Var(所以说甲公司XX年所有的这两个Var都是这个Industry和相应的投资数)。
依此类推SECOND Industry。

非常急,在线等,求助,谢谢。
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
沙发
 楼主| 发表于 2010-8-23 11:33:22 | 只看该作者

Re: sas一个取最大值的问题[求助,急!!!]

如果你的意思是比较同一公司不同行业的排名
先sort
然后
[code:2gtk73c5]
proc freq data=test order=freq;
by company;  
tables industry;
weight times;
run;
[/code:2gtk73c5]
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
板凳
 楼主| 发表于 2010-8-23 12:01:30 | 只看该作者

Re: sas一个取最大值的问题[求助,急!!!]

我的表达能力果然还是很差诶,不好意思。。。

我是想要这个TOP Industry变成一个新的Variable,然后投资次数再一个新的Var。
依此类推SECOND Industry。

而且这段代码貌似得到的是总的一个FREQ是吧?
我想要得到的这两个VAR都是关于每年的呢?
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
地板
 楼主| 发表于 2010-8-23 13:47:20 | 只看该作者

Re: sas一个取最大值的问题[求助,急!!!]

[quote:19pzuka1]现在我要得到这些公司在[color=#FF0000:19pzuka1]这些年份之前[/color:19pzuka1]所有的投资行业中哪个最多[/quote:19pzuka1]
"这些年份之前"是什么意思,是每年的上一年还是特指的一年?
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
5#
 楼主| 发表于 2010-8-23 13:55:17 | 只看该作者

Re: sas一个取最大值的问题[求助,急!!!]

就是比如数据从1980年开始,我算1994年的这个值算的是从1980年到1994年间所有的投资在A/B/C...行业中的数量.
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
6#
 楼主| 发表于 2010-8-23 15:26:17 | 只看该作者

Re: sas一个取最大值的问题[求助,急!!!]

你是要对每一个日期前算总量么?还是固定时间段内的?还是所有的?另外,你的数据是year company industryA industryB ......这样的么?

你还是给几个example data说的会比较清楚。
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
7#
 楼主| 发表于 2010-8-23 15:53:10 | 只看该作者

Re: sas一个取最大值的问题[求助,急!!!]

我不确定是否实现了在并列值中随机排序,你测试看看吧
[code:2x1rsbjo]data raw;
    length COMPANY $1 YEAR $4 INDUSTRY $40;
    input;
    COMPANY=scan(_infile_,1,' ');
    YEAR=scan(_infile_,2,' ');
    INDUSTRY=substr(_infile_,8,anydigit(_infile_,8)-9);
    COUNT=input(scan(_infile_,-1,' '),best.);
datalines;
A 1999 Communications and Media 1
A 1999 Computer Software and Services 1
A 1999 Internet Specific 5
A 2000 Communications and Media 1
A 2000 Computer Software and Services 7
A 2000 Internet Specific 1
A 2001 Communications and Media 1
A 2001 Computer Software and Services 1
A 2001 Internet Specific 1
B 1999 Biotechnology 8
B 1999 Communications and Media 7
B 1999 Computer Hardware 2
B 1999 Computer Software and Services 23
B 1999 Consumer Related 23
B 1999 Industrial/Energy 12
B 1999 Internet Specific 21
B 1999 Medical/Health 7
B 1999 Other Products 34
B 1999 Semiconductors/Other Elect. 5
B 2000 Biotechnology 13
B 2000 Communications and Media 14
B 2000 Computer Hardware 5
B 2000 Computer Software and Services 34
B 2000 Consumer Related 12
B 2000 Industrial/Energy 19
B 2000 Internet Specific 56
B 2000 Medical/Health 11
B 2000 Other Products 25
B 2000 Semiconductors/Other Elect. 3
;
proc transpose data=raw out=temp(drop=_name_);
    var count;
    by company year;
    id industry;
run;
data temp(drop=i);
    do _n_=1 by 1 until(last.company);
        set temp;
        by company;
        array industry_count_ {*} _numeric_;
        array temp {10} _temporary_;
        do i=1 to dim(industry_count_);
            industry_count_(i)=sum(ifn(missing(industry_count_(i)),0,industry_count_(i)),temp(i));
            temp(i)=industry_count_(i);
        end;
        output;
    end;
run;
data temp(keep=company year industry_count_1 industry_count_2 industry_name_1 industry_name_2);
    set temp;
    array temp {*} _numeric_;
    array industry_count_ {10};
    array industry_name_ {10} $32;
    flag=ceil(ranuni(0)*10);
    do i=1 to dim(industry_count_);
        j=i+ifn(i ge flag,-flag,10-flag)+1;
        industry_count_(j)=temp(i);
        call vname(temp(i),industry_name_(j));
    end;
    do i=dim(industry_count_)-1 to 1 by -1;
        do j=1 to i;
            if industry_count_(j+1) gt industry_count_(j) then do;
                temp_num=industry_count_(j);
                industry_count_(j)=industry_count_(j+1);
                industry_count_(j+1)=temp_num;
                temp_chr=industry_name_(j);
                industry_name_(j)=industry_name_(j+1);
                industry_name_(j+1)=temp_chr;
            end;
        end;
    end;
run;[/code:2x1rsbjo]
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
8#
 楼主| 发表于 2010-8-23 17:28:18 | 只看该作者

Re: sas一个取最大值的问题[求助,急!!!]

不好意思 第一次上来提问 问得辞不达意。。。
我既要算每年该公司的情况,也要算这年前所有的情况。
楼上大侠的数据结构貌似是对的,代码…我要好好研究学习下呀~

贴个sample上来吧:
COMPANY        YEAR        INDUSTRY        COUNT
A        1999        Communications and Media        1
A        1999        Computer Software and Services        1
A        1999        Internet Specific        5
A        2000        Communications and Media        1
A        2000        Computer Software and Services        7
A        2000        Internet Specific        1
A        2001        Communications and Media        1
A        2001        Computer Software and Services        1
A        2001        Internet Specific        1
B        1999        Biotechnology        8
B        1999        Communications and Media        7
B        1999        Computer Hardware        2
B        1999        Computer Software and Services        23
B        1999        Consumer Related        23
B        1999        Industrial/Energy        12
B        1999        Internet Specific        21
B        1999        Medical/Health        7
B        1999        Other Products        34
B        1999        Semiconductors/Other Elect.        5
B        2000        Biotechnology        13
B        2000        Communications and Media        14
B        2000        Computer Hardware        5
B        2000        Computer Software and Services        34
B        2000        Consumer Related        12
B        2000        Industrial/Energy        19
B        2000        Internet Specific        56
B        2000        Medical/Health        11
B        2000        Other Products        25
B        2000        Semiconductors/Other Elect.        3
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
9#
 楼主| 发表于 2010-8-24 17:24:23 | 只看该作者

Re: sas一个取最大值的问题[求助,急!!!]

我用前辈给的代码貌似的确完成了排序工作(虽然最后那小段琢磨不出什么意思望指点小弟。。。)。

一个新问题(应该算个小问题):
如果出现并列(比如甲公司在XX年在A和B分别投了15次并列最多)怎么让它随机一个第一,一个第二(现在的情况默认编号大的第一,小的第二,分析的时候应该会不合理的)?

大谢!
[quote="hopewell":3gh3uac0]修改中...[/quote:3gh3uac0]
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
10#
 楼主| 发表于 2010-8-25 01:04:11 | 只看该作者

Re: sas一个取最大值的问题[求助,急!!!]

to hopewell
呃。。。大神阿,怎么和上次的那块砖长得很不同了涅orz
这个我又要研究好一阵了估计
其实上次的那个方法已经把排序问题解决了貌似,只差最后一步并列情况随机的工作了。。。

诶丫丫,又要满眼天书了@@
好弱。。。
回复 支持 反对

使用道具 举报

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

本版积分规则

QQ|小黑屋|手机版|Archiver|SAS中文论坛  

GMT+8, 2026-2-4 03:29 , Processed in 0.132742 second(s), 21 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表