DATA Step, Macro, Functions and more

change rows order

Reply
Frequent Contributor
Posts: 76

change rows order

Hi All,

 

I met a question here:

I have a dataset with 32 rows (not columns), and I want to change the order of rows by what I want. for example, if I have the dataset with order:

gender

age

lab1

lab2

But I want the row order like:

age

lab1

lab2

gender

any way can solve this?

 

Thanks,

Chen

PROC Star
Posts: 7,360

Re: change rows order

 

proc sort data=yourdatasetname;
  by yourcolumnname;
run;

Art, CEO, AnalystFinder.com

 

Frequent Contributor
Posts: 76

Re: change rows order

Hi ART297,

 

I don't want to change my columns order, I want to change rows order. Any idea?

 

Thanks,

Chen

PROC Star
Posts: 7,360

Re: change rows order

You said that those values were all in the same column, but on different rows. If it's a SAS dataset, the column has to have a name.

 

If that doesn't answer your question, post an example of your file as a SAS data step.

 

Art, CEO, AnalystFinder.com

Valued Guide
Posts: 505

Re: change rows order

HAVE
====

Up to 40 obs WORK.HAVE total obs=4

Obs    GRP

 1     gender
 2     age
 3     lab1
 4     lab2

WANT
====

Up to 40 obs WORK.WANT total obs=4

Obs    GRP

 1     age
 2     lab1
 3     lab2
 4     gender

FULL SOLUTION
=============

* create some data;

data have;
input grp $;
cards4;
gender
age
lab1
lab2
;;;;
run;quit;

proc format;
  value $grp2odr
  'age'    = '01'
  'lab1'   = '02'
  'lab2'   = '03'
  'gender' = '04'
;run;quit;

proc sql;
  create
    table want as
  select
    *
  from
    have
  order
    by put(grp,$grp2odr.)
;quit;


PROC Star
Posts: 288

Re: change rows order

I think the better approach is with informat and formats, but just strictly with respect to the question of whether you can get what you want with the data you have, you can do an order by in proc sql where you sort on a boolean value first to force the order you want:

 

 

data have;
input var $;
datalines;
gender
age
lab1
lab2
;

proc sql;
    CREATE TABLE want AS
    SELECT *
    FROM have
    ORDER BY var eq 'gender', var;
quit; 
Ask a Question
Discussion stats
  • 5 replies
  • 246 views
  • 1 like
  • 4 in conversation