BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Emma_at_SAS
Lapis Lazuli | Level 10

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;
The dataset have
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
1 ACCEPTED SOLUTION

Accepted Solutions
Rydhm
Obsidian | Level 7

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.

View solution in original post

2 REPLIES 2
Rydhm
Obsidian | Level 7

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.

Emma_at_SAS
Lapis Lazuli | Level 10

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
 
How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1186 views
  • 2 likes
  • 2 in conversation