Hi,
How do I create a new variable that contains the number of measurement of each subject?
In below case, subject ID#4 has 3 repeated measurements so 3 should be assigned to his/her number of measurement, and subject ID#7 had 2 repeated measurements so 2 should be assigned as the number of measurement, subject ID#1 has only 1 measurement so 1 should be assigned to the new variable.
Subject ID | measurement |
1 | 1 |
2 | 1 |
3 | 1 |
4 | 1 |
4 | 2 |
4 | 3 |
5 | 1 |
6 | 1 |
7 | 1 |
7 | 2 |
8 | 1 |
9 | 1 |
10 | 1 |
11 | 1 |
12 | 1 |
12 | 2 |
13 | 1 |
13 | 2 |
14 | 1 |
15 | 1 |
Thank you in advance!
Something like this?
DATA have;
INPUT SubjectID measurement;
cards;
1 1
2 1
3 1
4 1
4 2
4 3
5 1
6 1
7 1
7 2
8 1
9 1
10 1
11 1
12 1
12 2
13 1
13 2
14 1
15 1
;
RUN;
PROC SORT DATA = have;
BY SubjectID;
RUN;
PROC PRINT;
RUN;
DATA want;
SET have;
BY SubjectID;
IF LAST.SubjectID;
RUN;
PROC PRINT;
RUN;
Something like this?
DATA have;
INPUT SubjectID measurement;
cards;
1 1
2 1
3 1
4 1
4 2
4 3
5 1
6 1
7 1
7 2
8 1
9 1
10 1
11 1
12 1
12 2
13 1
13 2
14 1
15 1
;
RUN;
PROC SORT DATA = have;
BY SubjectID;
RUN;
PROC PRINT;
RUN;
DATA want;
SET have;
BY SubjectID;
IF LAST.SubjectID;
RUN;
PROC PRINT;
RUN;
Maybe creating the dataset you have could be avoided by using something like
proc summary data=original nway;
class SubjectID;
output out=measured(drop= _type_ rename=(_freq_=measurement));
run;
with the data used to create the dataset you have shown.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.