Hello! I'm starting with sas base and I have a question.
I'm not able to find the way to create a new column which categorizes different words form other column in the same dataset.
I need to create a column (named category) with 3 different variables: "Clothes", "Bags" and "Other". So when the main column says t-shirt in the new category colum would say "clothes" if it says shoes the new columns would say other, etc
Clothes would search words like t-shirts, hats, etc
bags would search bagpacks, packs, etc
and other the rest of words
Thank you for the help!
Be aware of handling different cases of text like this:
data want;
set have;
if lowcase(main_column) in ('t-shirts', 'hoodies', 'hats') then do;
new_column = 'Clothes';
end;
else if lowcase(main_column) in ('bags', 'backpacks') then do;
new_column = 'Bags';
end;
else do;
new_column = 'Other';
end;
run;
Something like this? 🙂
data want;
set have;
if main_column in ('T-Shirt' /*, Insert other types of clothes here separated by , */) then do;
new_column = 'Clothes';
end;
else if main_column in ('Shoes') then do;
new_column = 'Other';
end;
else if main_column in ('Some Bags Name') then do;
new_column = 'Bags';
end;
run;
Not sure I understand completely, but you can put as many words inside the parenthesis as you want as below? 🙂
data want;
set have;
if main_column in ('T-Shirt' /*, Insert other types of clothes here separated by , */) then do;
new_column = 'Clothes';
end;
else if main_column in ('Shoes', 'Socks', 'Ties') then do;
new_column = 'Other';
end;
else if main column in ('Some Bags Name') then do;
new_column = 'Bags';
end;
run;
Ah, now I get it 🙂 Sure:
data want;
set have;
if main_column in ('T-shirts', 'hoodies', 'hats') then do;
new_column = 'Clothes';
end;
else if main_column in ('bags', 'backpacks') then do;
new_column = 'Bags';
end;
else do;
new_column = 'Other';
end;
run;
Anytime. Please mark my answer as a solution if it solved your problem 🙂 Regards.
Be aware of handling different cases of text like this:
data want;
set have;
if lowcase(main_column) in ('t-shirts', 'hoodies', 'hats') then do;
new_column = 'Clothes';
end;
else if lowcase(main_column) in ('bags', 'backpacks') then do;
new_column = 'Bags';
end;
else do;
new_column = 'Other';
end;
run;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.