DATA Step, Macro, Functions and more

To create a new reverse variable

Accepted Solution Solved
Reply
Contributor
Posts: 21
Accepted Solution

To create a new reverse variable

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!


Accepted Solutions
Solution
‎02-02-2016 09:23 AM
Respected Advisor
Posts: 4,919

Re: To create a new reverse variable

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


All Replies
Solution
‎02-02-2016 09:23 AM
Respected Advisor
Posts: 4,919

Re: To create a new reverse variable

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
Contributor
Posts: 21

Re: To create a new reverse variable

Thank you very much,PG!It works well!
Super User
Posts: 19,770

Re: To create a new reverse variable

Salary_Reverse=500-Salary;
Contributor
Posts: 21

Re: To create a new reverse variable

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!
Contributor
Posts: 29

Re: To create a new reverse variable

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;

 
☑ This topic is solved.

Need further help from the community? Please ask a new question.

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