how to sort variable by character variable first then numeric

how to sort variable by character variable first then numeric

Post by Mterjeso » Mon, 28 Apr 2008 01:20:31


Hi Karen,

There are many variations but here are a couple.


data sample;
StudentID='A1'; Score=78; Gender='M';
Address='123 Cherry Tree Lane'; output;
StudentID='B2'; Score=79; Gender='F';
Address='456 Cherry Tree Lane'; output;
run;

We can order the columns specifically, AND
we can order the columns by CHARACTER then NUMERIC.
Below are examples of both.


There are several ways to order the column
in specific order:

* SQL order as you list them ;
proc sql;
create table result1 as
select
StudentID,
Gender,
Address,
Score
from
sample
;
quit;

* datastep compiler order them in ;
* the order they are incurred. ;
* i.e. LENGTH found before SET. ;
data result2;
length StudentID $2
Gender $1
Address $20
Score 8
;
set sample;
run;


Here is one way to order by CHAR then NUM.

* ordering by character then numeric ;
data result3;
set sample(keep=_character_);
set sample(keep=_numeric_);
run;




Hope this is helpful.


Mark Terjeson
Senior Programmer Analyst
Investment Management & Research
Russell Investments


Russell Investments
Global Leaders in Multi-Manager Investing






-----Original Message-----
From: SAS(r) Discussion [mailto: XXXX@XXXXX.COM ] On Behalf Of
Karen
Sent: Saturday, April 26, 2008 9:00 AM
To: XXXX@XXXXX.COM
Subject: how to sort variable by character variable first then numeric
variable

Hi,

who knows if we can use SAS to sort the variables by character
variable first and then numeric variable.

Such as:
The original order is:
StudentID Score Gender Address

I want to sort it as

StudentID Gender Address Score

Any hint?

Thanks very much!
 
 
 

how to sort variable by character variable first then numeric

Post by muthia.kac » Mon, 28 Apr 2008 01:26:26


Whatever order of variables, irrespective of the type (string or number),
can be used with BY statement .

Muthia Kachirayan

 
 
 

how to sort variable by character variable first then numeric

Post by art29 » Mon, 28 Apr 2008 02:56:42

Karen,

Assuming that Mark is correct and you want to change the order of your
variables, rather than sort the data, then you could also accomplish it
with a retain statement before your set statement. E.g.,

data sample;
StudentID='A1'; Score=78; Gender='M';
Address='123 Cherry Tree Lane'; output;
StudentID='B2'; Score=79; Gender='F';
Address='456 Cherry Tree Lane'; output;
run;

data want;
retain StudentID Gender Address Score;
set sample;
run;

Art
-------