SAS中文论坛

标题: 解一道SAS题 [打印本页]

作者: shiyiming    时间: 2010-6-23 00:12
标题: 解一道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
作者: shiyiming    时间: 2010-6-23 10:03
标题: 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]
作者: shiyiming    时间: 2010-6-23 10:59
标题: 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]
作者: shiyiming    时间: 2010-6-23 11:46
标题: 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]
作者: shiyiming    时间: 2010-6-23 13:20
标题: Re: 解一道SAS题
data b;
   set a nobs=ncount;
   if _n_<=3 then group=2;
   else if _n_>(ncount-3) then group=1;
run;
好像不太对
作者: shiyiming    时间: 2010-6-23 13:25
标题: Re: 解一道SAS题
to hopewell
呵呵,十分感谢了
作者: shiyiming    时间: 2010-6-23 13:29
标题: Re: 解一道SAS题
to hopewell
很奇怪,你怎么知道啊
作者: shiyiming    时间: 2010-6-23 13:45
标题: Re: 解一道SAS题
Byes是对的,我在读的时候没有分开 if _n_ <=3 then group=1; 运行结果显示不出来,郁闷了好一会儿




欢迎光临 SAS中文论坛 (https://mysas.net/forum/) Powered by Discuz! X3.2