Date: Thu, 1 Oct 2009 08:20:01 -0700
Reply-To: demin <qdmiris@GMAIL.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: demin <qdmiris@GMAIL.COM>
Organization: http://groups.google.com
Subject: Re: How to add a column with order(1,2,3...) like obs in print?
Content-Type: text/plain; charset=ISO-8859-1
On Oct 1, 10:44 am, snoopy...@GMAIL.COM (Joe Matise) wrote:
> Are you in a data step or PROC SQL? In a data step, _N_ will (often) give
> you the row number, or more precisely the number of iterations through the
> data step loop. So:
> data test;
> set sashelp.class;
> obsnum=_n_;
> run;
>
> works, as long as you don't futz with other loops around the set statement.
>
> In PROC SQL, unofficially, monotonic() gives you the row number, though it's
> not guaranteed to work and is suggested not to use in production code.
>
> If you need to order by the column you posted, then extract the numeric
> portion at the end. Something like
> ORDER BY INPUT(SUBSTR(var,7),BEST12.)
> will work, in the given example. Like so:
>
> data test;
> input id $;
> datalines;
> Q12P3R1
> Q12P3R10
> Q12P3R2
> Q12P3R3
> ;;;;
> run;
>
> proc sql;
> select * from test order by input(substr(id,7),BEST12.);
> quit;
>
> If your id variable is not so easy to parse [or needs ordering by other
> parts] you may need to do something more complex, in that case you should
> post to the list a more accurately detailed list of potential IDs.
>
> -Joe
>
>
>
> On Thu, Oct 1, 2009 at 9:24 AM, demin <qdmi...@gmail.com> wrote:
> > Because I did something and made the order of my table has been
> > changed, so I would like to add a order column to keep the old order.
> > How to add such column with order 1,2,3...?
>
> > Actually the column is character type, like :
> > Q12P3R1
> > Q12P3R10 ? shis should be placed on the last one, but
> > Q12P3R2
> > Q12P3R3
> > ...
> > ?Q12P3R10 should be placed on the last one, but by "proc sql select
> > *... order by ..." till cannot get the order I want. how to solve
> > this? thanks.- Hide quoted text -
>
> - Show quoted text -
Thanks,
for the method in PROC SQL as you said, it won't work when the column
is more conplecated like:
Q12P3R1
Q12P3R10
Q12P3R2
Q12P3R3
Q12P4R1
Q12P5R1
... but now I learn this may use in other place.
About the method in Data step, I am trying. Thanks.
|