I created a flag variable like the below
data da1;
set da;
where visitnum gt 3;
if dastresn gt 0 then do; FLAG="Y"; FLAGN=1; end;
if dastresn le 0 then do; FLAG="N"; FLAGN=0; end;
run;
Now for some subjs i get both Y and N and in those cases i only need Y.
I used nodupkey to get unique subjects but in some subjects i get flag as N
Can you provide some sample data for your problem? 🙂
sub visit flag
1 3 Y
1 4 Y
2 4 N
2 5 N
2 6 Y
3 4 Y
In the above if i want unique sub i do nodupkey on sub but for sub 2 the flag is N and Y and i only get N as it is first. FOr sub 2 i want Y if it has both N and Y
Thats not a nodupkey then, nodupkey removes duplicate information based on the by grouping. Yours is a logical problem. I would say (and as there is no test data in the form of a datastep this is just theory):
sort your data with your flag in descending order on flag
do a datastep by subjid, if last.subjid then output
This way if Y exists it will be first.subjid due to the sort, otherwise N will be there.
proc sort data=have;
by sub flag;
run;
data want;
set have;
by sub;
if last.sub;
run;
Since 'Y' comes after 'N' in the sort order, this gets what you want.
If you want the last visit where flag was 'Y', add visit to the by statement in the sort.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.