I have a data set with the list of food items that kids ate during the day. For each category, for example, vegetables, there are words that are misspelled. I created a frequency for the variable vegetable and identified the misspelled words that I need to correct. I want to keep the correct reports as is. The way I introduce ELSE, my code only applies the last IF for lettuce and replaces cabbage with the original cabage. Thanks for your help.
data have;
length vagatables $ 20;
length fruits $ 20;
input vagetables $ fruits $ ;
datalines;
cabage orange
cabbage apple
carrot strwberry
lettuce apple
Letuce cherry
;
run;
proc print data=have; run;
data want;
set have;
if vagetables = "cabage" then vagetables_new="cabbage";
if vagetables = "Letuce" then vagetables_new="lettuce";
else vagetables_new=vagetables;
run;
proc print data=want; run;
Obs | vagetables | fruits |
---|---|---|
1 | cabage | orange |
2 | cabbage | apple |
3 | carrot | strwberry |
4 | lettuce | apple |
5 | Letuce | cherry |
The dataset want
Obs | vagetables | fruits | vagetables_new |
---|---|---|---|
1 | cabage | orange | cabage |
2 | cabbage | apple | cabbage |
3 | carrot | strawberry | carrot |
4 | lettuce | apple | lettuce |
5 | Letuce | cherry | lettuce |
You need to use else if
if vagetables = "cabage" then vagetables_new="cabbage";
else if vagetables = "Letuce" then vagetables_new="lettuce";
else vagetables_new=vagetables;
The last else otherwise which change all values to old ones.
You need to use else if
if vagetables = "cabage" then vagetables_new="cabbage";
else if vagetables = "Letuce" then vagetables_new="lettuce";
else vagetables_new=vagetables;
The last else otherwise which change all values to old ones.
Thank you, @Rydhm. It is fixed!
data want;
set have;
if vagetables = "cabage" then vagetables_new="cabbage";
if vagetables = "letuce" then vagetables_new="lettuce";
else if vagetables notin ("cabage","letuce") then vagetables_new=vagetables;
run;
proc print data=want; run;
The SAS System |
Obs | vagetables | fruits | vagetables_new |
---|---|---|---|
1 | cabage | orange | cabbage |
2 | cabbage | apple | cabbage |
3 | carrot | strwberry | carrot |
4 | lettuce | apple | lettuce |
5 | letuce | cherry | lettuce |
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.