BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Golf
Pyrite | Level 9
Suppose I have  3 variables  (X, Y, and Z)
    obs   X    Y     Z
      1    10    11     21
      2     2     12   22
      3   40   13    23
      4    4    14    24
      5   80    15    25
I want to create a new variable called XX that reverses the order of variable X while preserving the order of the other variables.
    obs   X   Y     Z     XX
      1     10  11    21     80
      2      2   12   22      4
      3    40  13   23    40 
      4      4   14   24      2
      5    80   15   25    10
 
Could you provide guidance on how to write SAS code to generate the second data set?
 
Thanks
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Is there anything in your dataset which denotes groups?

 

For the dataset as posted, I'd do

data second;
set have (keep=x rename=(x=xx));
n = _n_;
run;

proc sort data=second;
by n descending;
run;

data want;
merge
  have
  second (drop=n)
;
run;

Untested, posted from my tablet.

View solution in original post

8 REPLIES 8
Kurt_Bremser
Super User

Is there anything in your dataset which denotes groups?

 

For the dataset as posted, I'd do

data second;
set have (keep=x rename=(x=xx));
n = _n_;
run;

proc sort data=second;
by n descending;
run;

data want;
merge
  have
  second (drop=n)
;
run;

Untested, posted from my tablet.

FreelanceReinh
Jade | Level 19

Alternatively, you can use a second SET statement with a POINT= option:

data want;
set have nobs=n;
_n_=n-_n_+1;
set have(keep=x rename=(x=xx)) point=_n_;
run;
yabwon
Amethyst | Level 16

@FreelanceReinh, I didn't notice your solution and wrote almost exactly the same 😄 😄

 

Bart

 

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



yabwon
Amethyst | Level 16

You can use the POINt= for this (no sorting).

data have;
input X Y Z;
cards;
10    11     21
2     12   22
40   13    23
4    14    24
80    15    25
;
run;
proc print data=have;
run;


data want;
  set have nobs=nobs curobs=curobs;
  point=nobs-curobs+1;
  set have(keep=x rename=(x=xx)) point=point;
run;

proc print data=want;
run;

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Ksharp
Super User

Just for some fun. Here is  a SAS/IML solution.

 

data have;
input X Y Z;
cards;
10    11     21
2     12   22
40   13    23
4    14    24
80    15    25
;
run;
proc iml;
use have nobs nobs;
read all var {x y z};
close;
xx=x[nobs:1];
create want var {x y z xx};
append;
close;
quit;

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 8 replies
  • 2386 views
  • 4 likes
  • 5 in conversation