Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use label with if condition

Status
Not open for further replies.

blyssz

Technical User
Nov 18, 2008
49
US
I have a table with a column name Type which contain several colors like
Red, Green, Blue, White, Black etc.
Another column has variable var1,….var10 and for each color group I want to label the variable accordingly.

Type Variable
RED Var1
RED Var2
RED Var3
RED Var4
RED Var5
Green Var1
Green Var2
Green Var3
Green Var4
Green Var5
Blue Var1
Blue Var2
Blue Var3
Blue Var4
Blue Var5
I want to label Variables according to the type of color :
RED RedColor1
RED RedColor2
RED RedColor3
RED RedColor4
RED RedColor 5
Green GreenColor1
Green GreenColor 2
Green GreenColor3
Green GreenColor4
Green GreenColor5
Blue BlueColor1
Blue BlueColor 2
Blue BlueColor 3
Blue BlueColor 4
Blue BlueColor5

I need to write something like:
If Type=”Red” then do;
label Var1=”RedColor1”
Var2=”RedColor2”
End;

But that is not working;
Any suggestions?

Thanks,
Blyssz

 
I am not sure I understand your question. The LABEL statement is used to set the label of a variable(column). From your description it seems that you have a column that has a list of variables. Do you want to replace the string in that columns with your 'color' decriptors?

klaz
 
My question is :

For each type of color,I want to replace string var1, var2 as its color descriptors.
like for type "Red" , I want to replace Var1 as Redcolor1, Var2 as RedColor2 and so on.
It is possible to achieve this?

Thanks
Blyssz
 
Hi Blyssz,

Labels and variables values are two very different things in SAS. From your second post, it looks like you want to change the variable value.

This line may be what you are looking for:

Code:
color = cats(propcase(type),'Color',compress(variable,,'kd')) ;

An explanation:
The propcase function returns the first letter of a word in upper case and the rest of the word in lower case.

The compress function with the 'kd' modifier, will keep the numbers from the string - in this case the number from the variable column.

The cats function will then join the results of the two functions, with the string 'Color'.

Running the following code will demonstrate the output you will get in the variable called color.

Code:
data have ;
   input (type variable)(:$8.) ;
   length color $15 ;
   color = cats(propcase(type),'Color',compress(variable,,'kd')) ;
cards ;
RED     Var1
RED     Var2
RED     Var3
RED     Var4
RED     Var5
Green   Var1
Green   Var2
Green   Var3
Green   Var4
Green   Var5
Blue    Var1
Blue    Var2
Blue    Var3
Blue    Var4
Blue    Var5
;;;;
proc print; run ;

HTH
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top