DATA Step, Macro, Functions and more

I have a dataset which has flags and want to nodupkey them but have a problem

Reply
Frequent Contributor
Posts: 115

I have a dataset which has flags and want to nodupkey them but have a problem

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

PROC Star
Posts: 552

Re: I have a dataset which has flags and want to nodupkey them but have a problem

Can you provide some sample data for your problem? Smiley Happy

Frequent Contributor
Posts: 115

Re: I have a dataset which has flags and want to nodupkey them but have a 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

Super User
Super User
Posts: 7,411

Re: I have a dataset which has flags and want to nodupkey them but have a problem

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.

Super User
Posts: 6,948

Re: I have a dataset which has flags and want to nodupkey them but have a problem

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Ask a Question
Discussion stats
  • 4 replies
  • 177 views
  • 0 likes
  • 4 in conversation