BookmarkSubscribeRSS Feed
PriyaSaha
Calcite | Level 5

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

6 REPLIES 6
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

gsreddy
Fluorite | Level 6

step1:craete a new variable ,  a=_N_

step2: sort the above by variable a DESC;

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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. 

Patrick
Opal | Level 21

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.

Patrick
Opal | Level 21

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;

CTorres
Quartz | Level 8

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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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