BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Denali
Quartz | Level 8

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! 

1 ACCEPTED SOLUTION

Accepted Solutions
Norman21
Lapis Lazuli | Level 10

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;
Norman.
SAS 9.4 (TS1M6) X64_10PRO WIN 10.0.17763 Workstation

View solution in original post

2 REPLIES 2
Norman21
Lapis Lazuli | Level 10

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;
Norman.
SAS 9.4 (TS1M6) X64_10PRO WIN 10.0.17763 Workstation

andreas_lds
Jade | Level 19

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.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 993 views
  • 1 like
  • 3 in conversation