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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

2 REPLIES 2
ballardw
Super User

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;
Lijuu
Obsidian | Level 7
Thank you much in advance!!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

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