BookmarkSubscribeRSS Feed
vraj1
Quartz | Level 8

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

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

Can you provide some sample data for your problem? 🙂

vraj1
Quartz | Level 8

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Kurt_Bremser
Super User
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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 769 views
  • 0 likes
  • 4 in conversation