BookmarkSubscribeRSS Feed
spirto
Quartz | Level 8

Suppose I have a data set with three groups Abc,Def,Ghi Something like this:

Group X_var

A          3

A          5

A          2

B          1

B          12

B          6

B          78

C          99

C          4

The x variable doesn't really matter. I want to be able to write to the LOG "Name of Group1: Abc (n=3)" "Name of Group2: Def (n=4)" "Name of Group3: Ghi (n=2)".

Is there a way that I can automatically extract the different group names and their sample size (perhaps with a function)?? I should add that this will be part of a larger macro so it needs to be "macro-friendly".

1 REPLY 1
Haikuo
Onyx | Level 15

Not sure if this is you are after, also not sure what is "macro-friendly", all I know there is hardly any SAS code can NOT be wrapped up using macro.

data have;

input Group$ X_var;

cards;

A          3

A          5

A          2

B          1

B          12

B          6

B          78

C          99

C          4

;

data _null_;

do i=1 by 1 until (last.group);

   set have;

    by group notsorted;

end;

put "Name of Group" _n_ ":" group "(n=" i ")";

run;

   

Haikuo

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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
  • 1 reply
  • 1164 views
  • 0 likes
  • 2 in conversation