## Finding which subjects are valid for analysis

Solved
Regular Contributor
Posts: 181

# Finding which subjects are valid for analysis

[ Edited ]

Hello

I have a programming task which I find difficult, and I could use some help.

I have a table such as below, with ID being the ID of subjects in a clinical trial, type is a variable being either 1 or 2, V1-V6 are numerical variables, and visit, well, it's the visit, with Baseline being baseline and F1, F2,... are the follow up visits.

I want to create a dataset with all the subject ID's, where for each ID, I will have a variable marked as "yes" if he is in the analysis set, and "no" if he is not. A subject is in the analysis set, if he has a valid measurement at baseline AND has a valid measurement in at least one post baseline visit (F1, F2,...). A valid measurement is having no missing values in V1, V2 and V3 in case Type is 1, or having no missing values in V4, V5 and V6 in case Type is 2.

The outcome should be a dataset with 2 variables, ID and a binary variable of yes/no (1/2).

I find it very complicated, but I do have a feeling that the solution is not that hard. Can you please assist me ? Thank you in advance.

I thought to use the function nmiss instead of V1, V2 and V3 (and the same for V4, V5 and V6), but the rest of the task looks complicated.

Accepted Solutions
Solution
‎01-19-2016 07:23 AM
Super User
Posts: 10,761

## Re: Finding which subjects are valid for analysis

``````data have;
input id type v1-v6 visit \$;
cards;
1 1 2 3 5 3 . . baseline
1 1 9 . 5 3 8 6 f1
1 1 2 3 5 3 9 9 f2
2 2 2 3 5 3 9 3 baseline
2 2 9 . 5 3 . 6 f1
2 2 2 . 5 . . 9 f2
;
run;
data want;
length flag \$ 4;
do until(last.id);
set have;
by id;
if visit='baseline' then do;
if type=1 and nmiss(of v1-v3)=0 then base=1;
if type=2 and nmiss(of v4-v6)=0 then base=1;
end;
else do;
if type=1 and nmiss(of v1-v3)=0 then post=1;
if type=2 and nmiss(of v4-v6)=0 then post=1;
end;
end;
flag=ifc((base and post),'yes','no');
keep id flag;
run;``````

All Replies
Super User
Posts: 10,209

## Re: Finding which subjects are valid for analysis

[ Edited ]

Assuming that the dataset is sorted correctly so we don't run into problems.

``````data want (keep=ID mark);
set have;
array tests {6} V1-V6;
retain mark basemeasure follmeasure;
by ID;
if first.ID then do;
mark = 2;
basemeasure = 0;
follmeasure = 0;
end;
if visit = "Baseline" then do;
do i = (type-1) * 3 + 1 to (type-1) * 3 + 3;
if tests {i} ne . then basemeasure = 1;
end;
else do;
do i = (type-1) * 3 + 1 to (type-1) * 3 + 3;
if tests {i} ne . then follmeasure = 1;
end;
end;
if last.ID then do;
if basemeasure + follmeasure = 2 then mark = 1;
output;
end;
run;``````

Haven't run a test yet, need to write a data step first to create test data.

(You might consider adding a data step to future questions that creates test data with a cards; statement. Makes it easier for the helpers to run tests)

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Solution
‎01-19-2016 07:23 AM
Super User
Posts: 10,761

## Re: Finding which subjects are valid for analysis

``````data have;
input id type v1-v6 visit \$;
cards;
1 1 2 3 5 3 . . baseline
1 1 9 . 5 3 8 6 f1
1 1 2 3 5 3 9 9 f2
2 2 2 3 5 3 9 3 baseline
2 2 9 . 5 3 . 6 f1
2 2 2 . 5 . . 9 f2
;
run;
data want;
length flag \$ 4;
do until(last.id);
set have;
by id;
if visit='baseline' then do;
if type=1 and nmiss(of v1-v3)=0 then base=1;
if type=2 and nmiss(of v4-v6)=0 then base=1;
end;
else do;
if type=1 and nmiss(of v1-v3)=0 then post=1;
if type=2 and nmiss(of v4-v6)=0 then post=1;
end;
end;
flag=ifc((base and post),'yes','no');
keep id flag;
run;``````
Regular Contributor
Posts: 181

## Re: Finding which subjects are valid for analysis

Thank you to both of you. Your different approaches are both looking good. I tried the second, it works like charm. I will try the first as well for learning purposes as it looks helpful.

Thank you very much.

🔒 This topic is solved and locked.