Reputation: 143
I want a code to replace the numerics into character in the column.
For example :0-"No 1-"Yes".
I tried using if/else but its not working!!!
Data Assign2.Grocery_coup_two;
Set Assign2.Grocery_coupons;
If Heath_food_Store = 0
then Health_food_Store = "No";
Else Health_food_Store = "Yes";
Run;
Health_Food_Store
0
0
0
0
1
0
1
0
0
0
1
0
1
0
0
1
Upvotes: 2
Views: 12118
Reputation: 160
One solution is using SAS format. Just like how you might format numeric variables as dates (e.g. DATE9. is popular) you can create your own formats.
This is extremely useful if you don't want to repeat your if...then statements through out your codes.
In your case a solution like below may be useful.
Proc format;
value $YN
1 = 'Yes'
0 = 'No'
;
run;
Data Assign2.Grocery_coup_two;
Set Assign2.Grocery_coupons;
format Health_food_Store $YN.;
run;
The proc format
statement creates the new format $YN
which is applied to Health_food_Store
with the set
condition.
Upvotes: 1
Reputation: 189
Your solution is not working because you're trying to convert a numeric variable into a string variable. One way to fix this is to rename your numeric variable into _old, and then run the if-else statement:
Data Assign2.Grocery_coup_two;
Set Assign2.Grocery_coupons (rename = (Health_food_Store = Health_food_Store_old));
If Heath_food_Store_old = 0
then Health_food_Store = "No";
Else Health_food_Store = "Yes";
DROP Health_food_Store_old;
Run;
Upvotes: 5