Calcite | Level 5

## 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

6 REPLIES 6
Diamond | Level 26

## Re: query

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.

Fluorite | Level 6

## Re: query

step1:craete a new variable ,  a=_N_

step2: sort the above by variable a DESC;

Diamond | Level 26

## 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.

Opal | Level 21

## 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.

Opal | Level 21

## Re: query

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;

Quartz | Level 8

## 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

Discussion stats
• 6 replies
• 1258 views
• 5 likes
• 5 in conversation