SAS中文论坛

 找回密码
 立即注册

扫一扫,访问微社区

查看: 846|回复: 0
打印 上一主题 下一主题

sas test--求每个班级各个科目的最高分

[复制链接]

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
楼主
 楼主| 发表于 2011-5-7 17:51:35 | 只看该作者

sas test--求每个班级各个科目的最高分

From SAS_Miner's blog on Sina

<p><font COLOR="#0000FF">已知:数据集score记录的是每个班级各个科目(语文、数学)的三个最高分<br />
要求:生成数据集result(按班级横向排列各个科目的三个高分成绩)</FONT></P>
<p><font COLOR="#000000"><b>data</B> score;</FONT></P>
<p><font COLOR="#000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
input class rank chinese maths;</FONT></P>
<p><font COLOR="#000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
cards;</FONT></P>
<p><font COLOR="#000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
1 1 100 98</FONT></P>
<p><font COLOR="#000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
1 2 97 95</FONT></P>
<p><font COLOR="#000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
1 3 95 93</FONT></P>
<p><font COLOR="#000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
2 1 98 89</FONT></P>
<p><font COLOR="#000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
2 2 94 88</FONT></P>
<p><font COLOR="#000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
2 3 90 87</FONT></P>
<p><font COLOR="#000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
3 1 100 89</FONT></P>
<p><font COLOR="#000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
3 2 90 86</FONT></P>
<p><font COLOR="#000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
3 3 89 83</FONT></P>
<p><font COLOR="#000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
;</FONT></P>
<p><font COLOR="#000000">result<br />
class chinese1 chinese2 chinese3 maths1 maths2 maths3<br />
1 100 97 95 98 95 93<br />
2 98 94 90 89 88 87<br />
3 100 90 89 89 86 83</FONT></P>
<p><font COLOR="#0000FF">&nbsp;</FONT></P>
<p><font COLOR="#0000FF">法一:</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF"><b>data</B>
result;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">retain class;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">do i=<b>1</B> to
<b>3</B>;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">array c(<b>3</B>)
chinese1-chinese3;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">array m(<b>3</B>)
maths1-maths3;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">set score ;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">c(i)=chinese;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">m(i)=maths;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">end;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">drop i chinese maths rank
;</FONT></P>
<p><font COLOR="#0000FF"><b>run</B>;</FONT></P>
<p><font COLOR="#0000FF">&nbsp;</FONT></P>
<p><font COLOR="#0000FF">法 二:</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">***&nbsp;&nbsp; tranpose
macro</FONT></P>
<p><font COLOR="#0000FF"><b>%macro</B>
transpose(indate,i,n,class);</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">%do m=<b>1</B> %to
&amp;n;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">proc transpose
data=&amp;indate
out=score%scan(&amp;i,&amp;m)(drop=_name_)
prefix=%scan(&amp;i,&amp;m);</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">by
&amp;class;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">var
%scan(&amp;i,&amp;m);</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">run;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">%end;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">data result;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">merge</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">%do j=<b>1</B> %to
&amp;n;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">score%scan(&amp;i,&amp;j)</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">%end;;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">by
&amp;class;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">run;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF"><b>%mend</B> ;</FONT></P>
<p><font COLOR="#0000FF">%<b><i>transpose</I></B>(score,chinese
maths,<b>2</B>,class);</FONT></P>
<p><font COLOR="#0000FF">法三:</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">merge macro</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">*************************indate输入数据集;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">************************v
需转置的变量中间用空格隔开;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">***********************n
需转置的变量个数;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">***********************class
分组变量;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">**********************r
每组里的不同次数;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF"><b>%macro</B> merge
(indate,v,n,class,r);</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">proc sql noprint;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">select count(distinct
&amp;r) into :rank from
&amp;indate;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">quit;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">data result;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">merge</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">%do i=<b>1</B> %to
&amp;rank;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">score(rename=(%do j=<b>1</B>
%to &amp;n ;
%scan(&amp;v,&amp;j)=%scan(&amp;v,&amp;j)&amp;i.
%end;&nbsp;&nbsp; )
where=(&amp;r=&amp;i))</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">%end;;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">by
&amp;class;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">drop
&amp;r;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">run;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">data result;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">retain class %do i=<b>1</B>
%to &amp;n; %do j=<b>1</B> %to
&amp;rank;&nbsp;
%scan(&amp;v,&amp;i)&amp;j %end
;%end;;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">set result;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">run;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF"><b>%mend</B>;</FONT></P>
<p><font COLOR="#0000FF">%<b><i>merge</I></B>(score,chinese
maths,<b>2</B>,class,rank);</FONT></P>
<p><font COLOR="#0000FF">法四:</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF"><b>%macro</B>
arr(indate,v,n,r,class);</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">data result;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">retain
&amp;class;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">%do i=<b>1</B> %to
&amp;n;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">array
c&amp;i(&amp;r)</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">%do j=<b>1</B> %to
&amp;r;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">%scan(&amp;v,&amp;i)&amp;j
%end ;;%end;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">do k=<b>1</B> to
<b>3</B>;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">set
&amp;indate;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">%do m=<b>1</B> %to
&amp;n;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">c&amp;m.(k)=%scan(&amp;v,&amp;m.);%end;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">end;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">keep &amp;class
%do i=<b>1</B> %to &amp;n; %do j=<b>1</B> %to
&amp;r;&nbsp;
%scan(&amp;v,&amp;i)&amp;j %end
;%end;;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">run;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF"><b>%mend</B>;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">%<b><i>arr</I></B>(score,chinese maths,<b>2</B>
,<b>3</B>,class);</FONT></P>
<p><font COLOR="#0000FF">法五:</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF"><b>proc</B> <b>sort</B>
data=score;by class rank;<b>run</B>;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF"><b>proc</B> <b>transpose</B>
data=score out=a;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">by class;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">var chinese maths;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF"><b>run</B>;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF"><b>data</B>
chinese(drop=_NAME_) ;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">set
a(where=(_NAME_='chinese') rename=(COL1=chinese1 COL2=chinese2
COL3=chinese3 ));</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF"><b>run</B>;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF"><b>data</B>
maths(drop=_NAME_) ;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">set a(where=(_NAME_='maths')
rename=(COL1=maths1 COL2=maths2 COL3=maths3 ));</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF"><b>run</B>;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF"><b>data</B> all;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">merge</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
chinese (in=a)</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
maths&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
(in=b)</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">by class;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">if a or b;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF"><b>run</B>;</FONT></P>
<p><font COLOR="#0000FF">法六:</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF"><b>proc</B> <b>transpose</B>
data=score out=score2 prefix=chinese;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
var chinese ;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
by class;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF"><b>run</B>;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF"><b>proc</B> <b>transpose</B>
data=score out=score3 prefix=maths;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
var maths;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
by class;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF"><b>run</B>;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF"><b>data</B>
score4(drop=_name_);</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
merge score2 score3;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF"><b>run</B>;</FONT></P>
<p><font COLOR="#0000FF">法七:</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF"><b>proc</B> <b>transpose</B>
data=score out=result;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
by class;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
var chinese maths ;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF"><b>run</B>;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF"><b>proc</B> <b>transpose</B>
data=result out=result2(rename=(_name_=name));</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
by class _name_;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
var col:;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF"><b>run</B>;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF"><b>proc</B> <b>transpose</B>
data=result2 out=result(drop=_name_
rename=(col1-col3=chinese1-chinese3
col4-col6=maths1-maths3));</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
by class;</FONT></P>
<p ALIGN="left"><font COLOR="#0000FF">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
var col1 ;</FONT></P>
<p><font COLOR="#0000FF"><b>run</B>;</FONT></P><div style="border-top: 1px solid rgb(203, 217, 217); padding-top: 20px; padding-bottom: 10px;">
<p><br><a href="http://move.blog.sina.com.cn/msnmove/index.php" target="_blank">MSN空间完美搬家到新浪博客!</a></p></div>
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-10 09:39 , Processed in 0.092807 second(s), 21 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

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