BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
EmilyAV
Calcite | Level 5

Hello, 

I have a long list of entries for a categorical variable classified into levels in a RTF document.

What is the most efficient way to reclassify each observation into a new numeric variable? There is a lot of variation in punctuation between apparently similar observations, which I've classified manually.

 

Sample code, which works, but is untenable for hundreds of individual iterations of each level...

 

data new;
set old;
length fruit_num 8.;
if fruit = 'Apple' or fruit = 'Apples' or fruit = 'apple' then fruit_num = 1;

if fruit = 'Orange' or fruit = 'Oranges' or fruit = ' organge' then fruit_num = 2;

else fruit_num = .;

run;

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
Make a mapping dataset like:

Apple 1
apples 1
applle 1
.........

and using Hash Table to solve it .

View solution in original post

3 REPLIES 3
Reeza
Super User
Make your RTF file an Excel file with one column having the original value and the second column having the new value.
Then merge your two tables together. You can help yourself do some of this programatically by also standardizing the values in some manner - ie ensuring all are lower case, remove 's' at the end to at least get started.

You can also use the IN operator:

if upcase(fruit) in: ('APPLE') then fruit_num=1;

Anything that starts with APPLE (regardless of case) will now get classified as fruit_num=1, even APPLEPIE. Note the colon and IN usage in that statement.
It will not capture lines like Organge though.
Ksharp
Super User
Make a mapping dataset like:

Apple 1
apples 1
applle 1
.........

and using Hash Table to solve it .

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 520 views
  • 0 likes
  • 3 in conversation