Help using Base SAS procedures

query

Reply
Occasional Contributor
Posts: 13

query

I have a input dataset like below

input dataset
Name
A
B
E
F
D
C

Need a datase like below

output dataset
Name
C
D
F
E
B
A

Please help me write this query.

Thanks in advance priya

Super User
Super User
Posts: 7,942

Re: query

Posted in reply to PriyaSaha

Hi,

I am afraid you have no option other than to put the values in yourself.  There appears to be no logical reason why A is last and C is first for instance, so you cannot write a logical step to do this.

So, putting the information in yourself, you have several options.  Datastep, if statements, user formats.  Me, I would go with an array:

data want;

     set have;

     array letters{26} ("C","D","F",...);  /* In the order you want */

     do I=1 to 26;

          if letters{I}=name then var_id=I;

     end;

run;

Then sort by var_id.

Occasional Contributor
Posts: 14

Re: query

Posted in reply to PriyaSaha

step1:craete a new variable ,  a=_N_

step2: sort the above by variable a DESC;

Super User
Super User
Posts: 7,942

Re: query

Unfortunately its not descending though, F - E - B?  It just looks random.  Unless there is further information - such as the data was sorted that way prior, then you could use _n_ and then re-sort back.  However that's not stated in the post.  And the question would be, why?  What value is there having some arbitrary sort order without logic.  If the data changes the order might change and then you would not be able to re-create it. 

Respected Advisor
Posts: 4,173

Re: query

I believe what proposes is to create a variable from the source data set which holds the original Obs number - and then to sort the data set using this obs number variable in descending order. This should work and create the original source data set in reversed.

Respected Advisor
Posts: 4,173

Re: query

Posted in reply to PriyaSaha

If this is just about getting your source data set in reversed order without having a variable for sorting then below code could do.

data have;

input Name $;

datalines;

A

B

E

F

D

C

;

run;

data want;

  do i=nobs to 1 by -1;

    set have nobs=nobs point=i;

    output;

  end;

  stop;

run;

Regular Contributor
Posts: 180

Re: query

I agree with Patrick. Gsreddy solution would be something like this:

data temp / view=temp;

  set sashelp.class;

  a=_N_;

run;

proc sort data=temp out=class(drop=a);

  by descending a;

run;

Ctorres

Ask a Question
Discussion stats
  • 6 replies
  • 317 views
  • 5 likes
  • 5 in conversation