BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
xxz3231
Calcite | Level 5

Hi,Everyone,

I have a question about how to create a new variable which is in the reverse order of another one;

for instance ,this is the dataset,

NAME   SALARY

JACK    100

LUCY    200

MIKE     300

KATE    400

 

What I need is to create a new variable say "SALARY_REVERSE" to make the dataset looks like the follows

 

NAME SALARY SALARY_REVERSE

JACK   100               400

LUCY   200              300

MIKE   300               200

KATE   400              100

 

Thank you very much!

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

Sounds silly Smiley Happy

 

proc sort data=have out=haver; by descending salary ; run;

data want;
set have;
set haver(keep=salary rename=salary=reverse_salary);
run;
PG

View solution in original post

5 REPLIES 5
PGStats
Opal | Level 21

Sounds silly Smiley Happy

 

proc sort data=have out=haver; by descending salary ; run;

data want;
set have;
set haver(keep=salary rename=salary=reverse_salary);
run;
PG
xxz3231
Calcite | Level 5
Thank you very much,PG!It works well!
Reeza
Super User
Salary_Reverse=500-Salary;
xxz3231
Calcite | Level 5
Hi,Reeza,thank you very much! But the real data is much more complicated, I need a more general solution,PGStats' solution works well,thank you again!
Wang_Yajun
Obsidian | Level 7

Hi,

Let's say the dataset name is test, my solution is below

 

*Label Observation Number;
data test;
set test;
N = _N_;
run;

*Reverse the Order;
proc sort data = test out = test_reverse; by decending _N_;run;

*Relabel Observation Number;
data test_reverse;
set  test_reverse;
N=_N_;
run;

*Merge the two datasets;
data test_combine;
set test test_reverse;
by N;
run;

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 2034 views
  • 0 likes
  • 4 in conversation