Programming the statistical procedures from SAS

Change values of the first variable based on values of the second variable

Accepted Solution Solved
Reply
Contributor
Posts: 22
Accepted Solution

Change values of the first variable based on values of the second variable

Hi Everyone!

How can i rename "A" in var1 into A1, A2 and A3 based on 1, 8, and 25 in var2?

i.e lets have:

var1  var2

A         1

A         8

A         25

B          3

c          5

A          1

A          8

A         25

into  

var1  var2

A1        1

A 2       8

A 3       25

B           3

c           5

A1         1

A 2        8

A 3       25

 

Thank you alot!


Accepted Solutions
Solution
‎05-08-2018 05:35 PM
Super User
Posts: 13,583

Re: Change values of the first variable based on values of the second variable

Not "renaming" that means something else in SAS regardless of how you use your variable var1.

 

One of the basic approaches to changing the value of a variable based on the value of a different variable is the IF /Then statement in a data step:

data want;

   set have;

   If var1='A' and var2=1 then var1='A1';

run;

 

Some potential gotchas exist with this though. The variable var1 will have a length associated with the variable. If the length is 1 (one) then attempting to add more characters at the end will fail as there is no room for the additional variable.

 

You can group similar values into a "DO block to avoid repeating some tests such as var1='A': The following code demonstrates that plus how to change the length of var1 if needed.

data want;
   length var1 $ 5;
   set have;
   If var1='A' then do;
      if var2=1 then var1='A1';
      if var2=8 then var1='A2';
      if var2=25 then var1='A3';
   end;
run;

View solution in original post


All Replies
Solution
‎05-08-2018 05:35 PM
Super User
Posts: 13,583

Re: Change values of the first variable based on values of the second variable

Not "renaming" that means something else in SAS regardless of how you use your variable var1.

 

One of the basic approaches to changing the value of a variable based on the value of a different variable is the IF /Then statement in a data step:

data want;

   set have;

   If var1='A' and var2=1 then var1='A1';

run;

 

Some potential gotchas exist with this though. The variable var1 will have a length associated with the variable. If the length is 1 (one) then attempting to add more characters at the end will fail as there is no room for the additional variable.

 

You can group similar values into a "DO block to avoid repeating some tests such as var1='A': The following code demonstrates that plus how to change the length of var1 if needed.

data want;
   length var1 $ 5;
   set have;
   If var1='A' then do;
      if var2=1 then var1='A1';
      if var2=8 then var1='A2';
      if var2=25 then var1='A3';
   end;
run;
Contributor
Posts: 22

Re: Change values of the first variable based on values of the second variable

Thank you much in advance!!
☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 105 views
  • 0 likes
  • 2 in conversation