BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
willy0625
Calcite | Level 5

Dear SAS users.

I am currently working with the attatched dataset called "test".

where the good and bad variables are binary.

I want to keep ONLY the groups that

- have a good flag in the first row

- have a bad flag in any row after the first row.

Also assign a string as a fourth variable that would have "Y" if they satify the above crieteria or "N" otherwise.

Many thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data want(drop=one two);
 one=1; two=1;
 do until(last.id);
  set test;
  by id notsorted; 
  if first.id and good ne 1 then one=0;
  if (not first.id and bad ne 1) or (first.id and last.id) then two=0;
 end;

 if one and two then flag='Y';
   else flag='N';

 do until(last.id);
  set test;
  by id notsorted; 
 output;
 end;
run;

Ksharp

View solution in original post

2 REPLIES 2
Ksharp
Super User

You don't post ouput you want to see. So

Assuming only good or bad can be 1.(i.e. good=1 bad=0   or   good=0  bad=1).

libname x v9 'c:\';
proc means data=x.test nway noprint;
 class id;
 var bad;
 output out=temp(drop=_type_ rename=(id=_id))  sum= /autoname;
run;
data want(drop=_: bad_sum);
 set x.test;
 retain flag ' ';
 if id ne lag(id) then do;
   flag='N';
   if good=1 then do;
   do i=1 to _nobs;
    set temp nobs=_nobs point=i;
    if id=_id and _freq_=bad_sum+1 and _freq_ ne 1 then do; flag='Y'; leave;end;
   end;
   end;
 end;
run;



Ksharp

Ksharp
Super User
data want(drop=one two);
 one=1; two=1;
 do until(last.id);
  set test;
  by id notsorted; 
  if first.id and good ne 1 then one=0;
  if (not first.id and bad ne 1) or (first.id and last.id) then two=0;
 end;

 if one and two then flag='Y';
   else flag='N';

 do until(last.id);
  set test;
  by id notsorted; 
 output;
 end;
run;

Ksharp

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 2 replies
  • 675 views
  • 3 likes
  • 2 in conversation