<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: change rows order in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/change-rows-order/m-p/340533#M77832</link>
    <description>&lt;P&gt;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:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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; &lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 13 Mar 2017 18:26:57 GMT</pubDate>
    <dc:creator>collinelliot</dc:creator>
    <dc:date>2017-03-13T18:26:57Z</dc:date>
    <item>
      <title>change rows order</title>
      <link>https://communities.sas.com/t5/SAS-Programming/change-rows-order/m-p/340505#M77823</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I met a question here:&lt;/P&gt;&lt;P&gt;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:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;gender&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;age&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;lab1&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;lab2&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;But I want the row order like:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;age&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;lab1&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;lab2&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;gender&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;any way can solve this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Chen&lt;/P&gt;</description>
      <pubDate>Mon, 13 Mar 2017 17:24:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/change-rows-order/m-p/340505#M77823</guid>
      <dc:creator>echoli</dc:creator>
      <dc:date>2017-03-13T17:24:20Z</dc:date>
    </item>
    <item>
      <title>Re: change rows order</title>
      <link>https://communities.sas.com/t5/SAS-Programming/change-rows-order/m-p/340509#M77825</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sort data=yourdatasetname;
&amp;nbsp; by yourcolumnname;
run;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Mar 2017 17:33:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/change-rows-order/m-p/340509#M77825</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-03-13T17:33:57Z</dc:date>
    </item>
    <item>
      <title>Re: change rows order</title>
      <link>https://communities.sas.com/t5/SAS-Programming/change-rows-order/m-p/340511#M77826</link>
      <description>&lt;P&gt;Hi ART297,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I don't want to change my columns order, I want to change rows order. Any idea?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Chen&lt;/P&gt;</description>
      <pubDate>Mon, 13 Mar 2017 17:36:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/change-rows-order/m-p/340511#M77826</guid>
      <dc:creator>echoli</dc:creator>
      <dc:date>2017-03-13T17:36:58Z</dc:date>
    </item>
    <item>
      <title>Re: change rows order</title>
      <link>https://communities.sas.com/t5/SAS-Programming/change-rows-order/m-p/340514#M77828</link>
      <description>&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If that doesn't answer your question, post an example of your file as a SAS data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;</description>
      <pubDate>Mon, 13 Mar 2017 17:40:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/change-rows-order/m-p/340514#M77828</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-03-13T17:40:49Z</dc:date>
    </item>
    <item>
      <title>Re: change rows order</title>
      <link>https://communities.sas.com/t5/SAS-Programming/change-rows-order/m-p/340515#M77829</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;


&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 13 Mar 2017 17:42:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/change-rows-order/m-p/340515#M77829</guid>
      <dc:creator>rogerjdeangelis</dc:creator>
      <dc:date>2017-03-13T17:42:25Z</dc:date>
    </item>
    <item>
      <title>Re: change rows order</title>
      <link>https://communities.sas.com/t5/SAS-Programming/change-rows-order/m-p/340533#M77832</link>
      <description>&lt;P&gt;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:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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; &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 13 Mar 2017 18:26:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/change-rows-order/m-p/340533#M77832</guid>
      <dc:creator>collinelliot</dc:creator>
      <dc:date>2017-03-13T18:26:57Z</dc:date>
    </item>
    <item>
      <title>Re: change rows order</title>
      <link>https://communities.sas.com/t5/SAS-Programming/change-rows-order/m-p/616387#M180443</link>
      <description>Hi, That's also a good approach. But what if we have 1000 observations. We can't give 01,02,03...and so on for all variables. Any approach to reorder such a large no. of observations.&lt;BR /&gt;&lt;BR /&gt;Thanks!</description>
      <pubDate>Fri, 10 Jan 2020 06:10:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/change-rows-order/m-p/616387#M180443</guid>
      <dc:creator>abhishekdixit93</dc:creator>
      <dc:date>2020-01-10T06:10:26Z</dc:date>
    </item>
    <item>
      <title>Re: change rows order</title>
      <link>https://communities.sas.com/t5/SAS-Programming/change-rows-order/m-p/616511#M180497</link>
      <description>&lt;P&gt;This thread is from 3 years ago. I don't know if you were the original poster, but neither you nor they (if it wasn't you) ever provided an example of what your actual data looked like.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have gender, age, lab1 and lab2 records (in that order) for each subject, and you simply want to move the gender record to be in the fourth position within each subject's collection of records, then the following should work:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* this first datastep is simply to create an example */
/* of what your data might currently look like */
data have;
  input var $ value $;
  datalines;
gender M
age 24
lab1 78
lab2 87
gender F
age 30
lab1 65
lab2 56
gender M
age 32
lab1 84
lab2 48
;
run;

data need;
  set have;
  if var eq "gender" then do;
    id+1;
    order=1;
  end;
  else order=0;
run;

proc sort data=need out=want (drop=order);
  by id order;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Jan 2020 15:30:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/change-rows-order/m-p/616511#M180497</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2020-01-10T15:30:03Z</dc:date>
    </item>
    <item>
      <title>Re: change rows order</title>
      <link>https://communities.sas.com/t5/SAS-Programming/change-rows-order/m-p/772626#M245346</link>
      <description>Thanks a lot. It was very useful.</description>
      <pubDate>Thu, 07 Oct 2021 04:58:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/change-rows-order/m-p/772626#M245346</guid>
      <dc:creator>DrAbhijeetSafai</dc:creator>
      <dc:date>2021-10-07T04:58:16Z</dc:date>
    </item>
  </channel>
</rss>

