DATA Step, Macro, Functions and more

How to automatically extract factor level names

Reply
Contributor
Posts: 38

How to automatically extract factor level names

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".

Respected Advisor
Posts: 3,124

Re: How to automatically extract factor level names

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

Ask a Question
Discussion stats
  • 1 reply
  • 167 views
  • 0 likes
  • 2 in conversation