n*****5 发帖数: 61 | 1 Consider the following dataset:
data StudentScore;
length studentID $ 1. year 3. score 3.;
input studentID year score;
datalines;
A 91 400
A 92 398
A 92 399
B 91 430
B 92 432
B 93 444
B 94 446
C 91 455
C 92 423
C 93 411
C 94 415
C 95 427
C 95 418
run;
Q1. Create a variable called “Flag” which indicates whether a student’s
score increased or decreased from the previous record in the data. Mark a “
0” for records where the student’s score was lower than the previous
record. Conversely, mark a “1” for records where a student’s score was
equal or higher than the previous record. Mark a “0” for the first record
of each student.
Q2. Write code to create a SAS dataset named “StudentScoreNew” that
contains each student’s most recent score. If there are multiple scores for
the most recent year, then pick the highest one for that year. |
a****y 发帖数: 1719 | 2 我也是刚学sas 试着写了一个,不知道对不对,你可以参考一下
data studentscore;
set studentscore;
by studentid;
retain temp flag;
if first.studentid then do;
temp=score;
flag=0;
end;
if temp < score then do;
flag=1;
temp=score;
end;
if temp > score then do;
flag=0;
temp=score;
end;
run; |
s****u 发帖数: 1200 | 3 好像不是和第一个比,而是和上一个比。用lag 省事
★ 发自iPhone App: ChineseWeb 7.8
【在 a****y 的大作中提到】 : 我也是刚学sas 试着写了一个,不知道对不对,你可以参考一下 : data studentscore; : set studentscore; : by studentid; : retain temp flag; : if first.studentid then do; : temp=score; : flag=0; : end; : if temp < score then do;
|
c*****i 发帖数: 1392 | 4 Q1:
data flag(drop=prevID prevscore);
set StudentScore;
if _n_=1 then flag=0;
else do;
set StudentScore(drop=year rename=(studentID=prevID score=prevscore));
if studentID ^= prevID then flag=0;
else if score
else flag=1;
end;
run;
Q2:
proc sort data=StudentScore;
by studentID year score;
run;
data StudentScoreNew;
set StudentScore;
by studentID year score;
if last.studentID;
run; |