BookmarkSubscribeRSS Feed
rebelde52
Fluorite | Level 6

Hello , 

 

How could I select Observations (Rows) where if their Pending column ever says "YES" then output to a new dataset? I am thinking to use proc sql but also, I'm still learning as I go so I feel stuck. Below is my have data and want data tables. 

 

Have

IDTest_DatePending
TA3/29/22NO
TA3/29/22NO
TA3/29/22YES
PG3/29/22NO
PG4/5/22NO
LT4/5/22YES
LT4/5/22NO
LT4/5/22NO

 

Want 

IDTest_DatePending
TA3/29/22NO
TA3/29/22NO
TA3/29/22YES
LT4/5/22YES
LT4/5/22NO
LT4/5/22NO
5 REPLIES 5
PaigeMiller
Diamond | Level 26

From now on, please provide data as working SAS data step code (examples and instructions) and not as Excel copy and paste. Also, you would make your own typing/coding simpler if YES/NO character strings were actually 0/1 numeric values.

 

proc sql;
	create table want as select *
	from have  	
	group by id
	having sum(pending='YES')>=1;
quit;

 

 

--
Paige Miller
mkeintz
PROC Star

This is a data step solution, using a hash object containing the only id's that have any instance of 'YES':

 

Untested, in the absence of sample data in the form of a working data step.

 

data want ;
  set have;
  if _n_=1 then do;
    declare hash ids_with_yes (dataset:'have (keep=id pending where=(pending="YES"))');
      ids_with_yes.definekey('id');
      ids_with_yes.definedata('id');
      ids_with_yes.definedone();
  end;
  if ids_with_yes.check()=0;
run;

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
yabwon
Onyx | Level 15

I did a test, works perfect (as usual).

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Kondala
Calcite | Level 5

data new;

set ...;

where ID= "PG";

run;

 

proc print data = new;

run;

yabwon
Onyx | Level 15

Hi @rebelde52 ,

 

@PaigeMiller  and @mkeintz already gave you great solutions, so I'm adding this on just for fun.

 

data have;
input ID $	Test_Date :mmddyy8.	Pending $;
format Test_Date mmddyy10.;
cards;
TA 3/29/22 NO
TA 3/29/22 NO
TA 3/29/22 YES
PG 3/29/22 NO
PG 4/5/22 NO
LT 4/5/22 YES
LT 4/5/22 NO
LT 4/5/22 NO
;
run;
proc print;
run;

data want;
  _N_=0;
  do until(last.ID);
    set have;
    by ID notsorted;
    _N_ + Pending="YES";  
  end;

  do until(last.ID);
    set have;
    by ID notsorted;
    if _N_ then output; 
  end;
run;
proc print;
run;

 

Bart

 

{EDIT:}

One more for "lazy typers":

%macro DoW(code);
  do until(last.ID);
    set have;
    by ID notsorted;
    &code.;  
  end;
%mend;

data want;
  _N_=0;
  %DoW(_N_ + Pending="YES")  
  %DoW(if _N_ then output) 
run;
_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1713 views
  • 4 likes
  • 5 in conversation