BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Sathish_jammy
Lapis Lazuli | Level 10

Dear Experts,

 

I need to fetch the IDs from my dataset based on group by condition. 

I'll share my sample data for your reference.

data have;
input ID Des1 Des2 Des3 Des4;
cards;
123 1 0 0 0
123 1 0 0 0
123 1 0 0 0
159 1 0 0 0
159 1 1 0 0
159 0 1 0 0
321 1 0 0 0
456 0 0 0 1
456 1 0 0 0
456 1 0 0 0
;

I expected output as IDs who are Deceased (Des1 =1) first and the rest of Des_ = 0.  (Output - 123, 321)

Kindly let me know for any clarification.

1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Onyx | Level 15

Hi @Sathish_jammy ,

 

Like this:

data have;
input ID Des1 Des2 Des3 Des4;
cards;
123 1 0 0 0
123 1 0 0 0
123 1 0 0 0
159 1 0 0 0
159 1 1 0 0
159 0 1 0 0
321 1 0 0 0
456 0 0 0 1
456 1 0 0 0
456 1 0 0 0
;
run;


data want;
  set have;
  by id;
  if first.id then elig = 1; retain elig;
  cond =  Des1=1 and sum(Des2, Des3, Des4) = 0 ;
  elig = elig * cond;
  if last.id and elig then output;
run;

?

 

All the best

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



View solution in original post

2 REPLIES 2
ed_sas_member
Meteorite | Level 14

Hi @Sathish_jammy 

 

Like this?

proc means noprint;
	var Des1 Des2 Des3 Des4;
	class id;
	ways 1;
	output out=have_stat (drop=_:) n= sum= /autoname;
run;

data have_flag;
	set have_stat;
	flag_des1 = 0;
	if Des1_N = Des1_Sum then flag_des1=1; /* Identifies id 123 and 321 for example*/
	/*you can compute the flag for Des2, ... also */
run;

proc sql;
	create table want as
	select a.*, b.flag_des1
	from have as a inner join have_flag as b
	on a.id=b.id
	order by b.flag_des1 desc, a.id;
quit;
yabwon
Onyx | Level 15

Hi @Sathish_jammy ,

 

Like this:

data have;
input ID Des1 Des2 Des3 Des4;
cards;
123 1 0 0 0
123 1 0 0 0
123 1 0 0 0
159 1 0 0 0
159 1 1 0 0
159 0 1 0 0
321 1 0 0 0
456 0 0 0 1
456 1 0 0 0
456 1 0 0 0
;
run;


data want;
  set have;
  by id;
  if first.id then elig = 1; retain elig;
  cond =  Des1=1 and sum(Des2, Des3, Des4) = 0 ;
  elig = elig * cond;
  if last.id and elig then output;
run;

?

 

All the best

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



sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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