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

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 760 views
  • 0 likes
  • 4 in conversation