DATA Step, Macro, Functions and more

How to output a variable containing numeric part in ascending order?

Reply
Occasional Contributor
Posts: 5

How to output a variable containing numeric part in ascending order?

Generally I want to use "class group;" to output means for "group1, group2, ....group10" like:

Group      mean    

group1       ..

group2      ..

...               ..

group9      ..

group10    ..

 

however, if using just "class group;", the output is like:

Group      Mean    

group1       ..

group10     ..

...                ..

group8       ..

group9       ..

 

group10 was not output at the bottom, but instead, it showed after group1. 

How can it be coded so that group10 is at the bottom. thank you very much.

 

Super User
Posts: 10,497

Re: How to output a variable containing numeric part in ascending order?

[ Edited ]

This is a common issue with character variables. You have  couple of options. One is the be very conscious of using Group01 instead of Group1 though that may be difficulate to use as an approach if you don't know ahead of time that you're going to need Group0001 ...

The next to create your summaries into a data set. Then use proc sort with the SORTSEQ option Numeric_collation=On

 

proc sort data=have sortseq=linguistic(numeric_collation='ON'); by group;run;

and then print.

 

I'm not sure if any of the report procedures directly support the option.

 

The other approach is create a variable that always sorts in the order you want and then use a custom display format to diplay the desired value. Some times the new variable is easiest to create with a custom informat:

 

proc format ;

invalue group

"group1" = 1

"group2" = 2

...

"group10" = 10;

value group

1 = "group1"

2 = "group2"

...

10= "group10"

;

run;

 

data want;

   set have;

   newgroup = input(group,group.);

   format newgroup group.;

run;

 

proc means data=want ;

   class newgroup /order=unformatted;

   var analysisvar;

run;

 

Occasional Contributor
Posts: 5

Re: How to output a variable containing numeric part in ascending order?

many thanks! Smiley Happy

Spoiler
 
Ask a Question
Discussion stats
  • 2 replies
  • 172 views
  • 1 like
  • 2 in conversation