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

Hello team

why don't I get a group by this code?

data test1;
input id sys dia;
datalines;
1 23 24
1 23 24
2 12 13
;
Run;

data grouping;
set test1;
by id sys dia;
run;

Am I not supposed to get only for rows for 1 23 24? Please advise me.

Regards,

Blueblue

Blue Blue
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

If you want to reduce the data to one observation per BY group you could use the NODUPKEY option on PROC SORT.

 

But if the goal is to sort by ID SYS DIA and then reduce it to one observation per value of ID then you can get the FIRST.ID automatic flag that the BY statement creates.

proc sort data=have ;
  by id sys dia ;
run;
data min_blood_pressure;
  set have;
  by id sys dia;
  if first.id;
run;

View solution in original post

5 REPLIES 5
yabwon
Onyx | Level 15

I don't get your question: "Am I not supposed to get only for rows for 1 23 24?", but if I were you I would start by reading the documentation:

https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lepg/n1ecf3exv3sn7bn1htxoz074c72z.htm

 

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



PaigeMiller
Diamond | Level 26

What do you mean by "group" here? This is not really a word that describes any SAS structure that I know of.

--
Paige Miller
Tom
Super User Tom
Super User

You are creating GROUPING as a copy of TEST1.  

The only thing the BY statement will do in that data step is insure that TEST1 is actually sorted by ID SYS DIA.  It for example an observation with ID=1 came after on observation with ID=2 it would generate an error since the that is not the sort order you told the BY statement to expect.

GN0001
Barite | Level 11

Hello all,

What I mean is to receive one observation for those rows that are same if those observations are sorted properly. I don't know what word I need to use instead of grouping. I know in SAS before grouping, I need to sort my data.

Thanks,

blueblue

Blue Blue
Tom
Super User Tom
Super User

If you want to reduce the data to one observation per BY group you could use the NODUPKEY option on PROC SORT.

 

But if the goal is to sort by ID SYS DIA and then reduce it to one observation per value of ID then you can get the FIRST.ID automatic flag that the BY statement creates.

proc sort data=have ;
  by id sys dia ;
run;
data min_blood_pressure;
  set have;
  by id sys dia;
  if first.id;
run;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 5 replies
  • 572 views
  • 3 likes
  • 4 in conversation