SAS中文论坛

 找回密码
 立即注册

扫一扫,访问微社区

查看: 1094|回复: 7
打印 上一主题 下一主题

解一道SAS题

[复制链接]

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
楼主
 楼主| 发表于 2010-6-23 00:12:39 | 只看该作者

解一道SAS题

Data set ‘students.txt’ contains variables name, social security number, and grades for the courses taken by several students.

a.) Using arrays, write a program that calculates the number of courses taken, the total scores earned, and the mean grade for each student

b.) After you get these results, use an indicator variable to indicate the data as two groups:
the first 3 in group 1, and the last 3 in group 2 (don’t put them into two data sets!).

c.) Use PROC IML to read the data from part b into matrices x1 and x2, where x1 and x2 are 3*3 matrices for the variables number of courses taken, total scores earned, and mean grade, respectively.
Only print out matrices 1 and 2.


students.txt:
Smith  888-55-1212 87.3 92.1 90.6 97.8 95.6 93.4 90.4
Jones  555-88-1234 69.2 73.8 82.6 76.5 84.3 90.1 78.7
Green  777-88-9999 86.9 89.0 92.5 81.6 79.4 85.5 82.1
Brown 888-99-1111 78.6 82.1   .     78.4 88.6 95.5 86.0
Young 777-66-5555 96.0 85.5 97.2 86.4 72.1 89.9 93.5
Meyer 888-55-4444 99.9 .       96.7 92.1    .    96.6 92.8
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
沙发
 楼主| 发表于 2010-6-23 10:03:19 | 只看该作者

Re: 解一道SAS题

不会IML
[code:2e4y9qsh]filename raw 'c:\students.txt';
data raw;
        length name ssn $ 10;
        infile raw dlm=' ';
        input name ssn grade1-grade7;
run;
data temp;
        set raw nobs=n;
        array grade{*} grade:;
        total=0;
        do number=0 to dim(grade)-1;
                total+grade(number+1);
        end;
        mean=total/number;
        group=ceil(_n_*2/n);
run;[/code:2e4y9qsh]
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
板凳
 楼主| 发表于 2010-6-23 10:59:14 | 只看该作者

Re: 解一道SAS题

[code:2b219qg8]
data source;
        infile cards;
        input name:$32. id :$11. grades1- grades7;
cards;
Smith 888-55-1212 87.3 92.1 90.6 97.8 95.6 93.4 90.4
Jones 555-88-1234 69.2 73.8 82.6 76.5 84.3 90.1 78.7
Green 777-88-9999 86.9 89.0 92.5 81.6 79.4 85.5 82.1
Brown 888-99-1111 78.6 82.1 . 78.4 88.6 95.5 86.0
Young 777-66-5555 96.0 85.5 97.2 86.4 72.1 89.9 93.5
Meyer 888-55-4444 99.9 . 96.7 92.1 . 96.6 92.8
;
run;

/*a)*/
data a;
        set source;
        array rr grades:;
        n=0;sum=0;
        do over rr;
                if not missing(rr) then do;n=n+1;sum=sum+rr;end;
        end;
        mean=ifn(n=0,.,sum/n);
run;

/*b)*/
proc sort data=a;
        by sum;
run;
data b;
        set a nobs=ncount;
        if _n_<=3 then group=2;
        else if _n_>(ncount-3) then group=1;
run;

/*c)*/
proc iml;
        use b;
        read all var{n sum mean} where(group=1) into x1;
        read all var{n sum mean} where(group=2) into x2;
        close b;
        print x1;
        print x2;
quit;

[/code:2b219qg8]
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
地板
 楼主| 发表于 2010-6-23 11:46:13 | 只看该作者

Re: 解一道SAS题

byes大侠是对的
忘了缺失值的事了...
[code:1aqtxyyh]%macro test(inds,species,symbol=star,color=red);
        %let species=%upcase(&species);
        data &inds;
                length species $ 2;
                set &inds;
                species=cats(substr(first_name,1,1),substr(last_name,1,1));
        run;
        title "SPECIES: ALL";
        proc freq data=&inds;
                tables species /nopercent nocum;
        run;
        data &species;
                set &inds;
                where upcase(species)="&species";
        run;
        title "SPECIES: &species";
        symbol1 color=&color value=&symbol height=3;
        proc gplot data=&species;
                plot y*x;
        run;
        quit;
%mend;

filename raw 'c:\plants.txt';
data raw;
        length first_name last_name $ 20;
        infile raw dlm=' ';
        input first_name last_name x y;
run;
%test(raw,cb)[/code:1aqtxyyh]
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
5#
 楼主| 发表于 2010-6-23 13:20:16 | 只看该作者

Re: 解一道SAS题

data b;
   set a nobs=ncount;
   if _n_<=3 then group=2;
   else if _n_>(ncount-3) then group=1;
run;
好像不太对
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

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

Re: 解一道SAS题

to hopewell
呵呵,十分感谢了
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
7#
 楼主| 发表于 2010-6-23 13:29:00 | 只看该作者

Re: 解一道SAS题

to hopewell
很奇怪,你怎么知道啊
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
8#
 楼主| 发表于 2010-6-23 13:45:39 | 只看该作者

Re: 解一道SAS题

Byes是对的,我在读的时候没有分开 if _n_ <=3 then group=1; 运行结果显示不出来,郁闷了好一会儿
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-4 04:57 , Processed in 0.080834 second(s), 20 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

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