BookmarkSubscribeRSS Feed
Yosef20
Calcite | Level 5

I am working with a sandwich shop's dataset and need to look within the Description column for the word 'Flatbread'. If the description contains flatbread, then I want the Item_Type column to return the value 'Sandwich'. There are over 500 items, and want it to look through all of the observations where Item_Type is blank for the word Flatbread. I have tried to do a findw command, and a contains command to get it to work - to no prevail. Any help would be greatly appreciated, I need to present my analysis very soon, so any prompt replies will be greatly appreciated. Thank you!

3 REPLIES 3
Reeza
Super User

FINDW() is the correct approach. Please post your code that doesn't work and we can see where it's wrong or if there's something else that needs to be factored in. Note that searches for character values are case sensitive. 

 

It also helps if you can post sample data, since your data structure since that may define how things need to be coded. 

 


@Yosef20 wrote:

I am working with a sandwich shop's dataset and need to look within the Description column for the word 'Flatbread'. If the description contains flatbread, then I want the Item_Type column to return the value 'Sandwich'. There are over 500 items, and want it to look through all of the observations where Item_Type is blank for the word Flatbread. I have tried to do a findw command, and a contains command to get it to work - to no prevail. Any help would be greatly appreciated, I need to present my analysis very soon, so any prompt replies will be greatly appreciated. Thank you!


 

Yosef20
Calcite | Level 5

What dataset looks like now:

 

ItemNumber     Description                   ItemType        

 

1                       Double Flatbread       |  <blank>

                         with chicken

2                       Regular Bacon ...       |  Sandwich

 

3                       Regular Turkey/Ham  | Sandwich 

 

- There are also ItemTypes such as salads, chips, cookies, add ons etc. but I am only testing sandwiches right now.

Example of what output dataset should look like:

 

ItemNumber     Description               ItemType        

 

1                       Double Flatbread     Sandwich

                         with chicken

etc.

 

Test Code (doesn't work):

 

DATA Work.TestData;

     SET Work.TestData;

           IF Item_Type = '' then DO;

                  IF FINDW(Description, 'Flatbread')>0 then Item_Type = 'Sandwich';

           END;

RUN;

 

The last forum I found the FINDW command was not very helpful in explaining the structure of it so I don't know if I even started doing it correctly.

 

Thank you for the help, if there is more code or data that you need to see I can get it.

Reeza
Super User
DATA Work.TestData2; *do not use the same name in DATA and SET statements. This makes it very hard to debug;

     SET Work.TestData;

           IF missing(ItemType) and FINDW(Description, 'Flatbread', 'i')>0 then ItemType = 'Sandwich';


RUN;

Try this. 

 

Note a few changes:

 

1. Item_TYPE to ItemTYpe - you seem to be using both but your data shows ITemType, not underscore. If this was the case the log would likely have an error about uninitialized variable. 

2. Replace testDATA in DATA statement (first line) so that it's easier to debug the code

3. Remove IF/THEN since not needed

4. Added 'i' modifier to FIND to allow it to ignore the case of the world flatbread.

 

Let me know if this works and if it doesn't post the full log. 

 

Documentation on the FIND function is here:

https://documentation.sas.com/?docsetId=lefunctionsref&docsetTarget=p16rdsa30vmm43n1ej4936nwa01t.htm...

 


@Yosef20 wrote:

What dataset looks like now:

 

ItemNumber     Description                   ItemType        

 

1                       Double Flatbread       |  <blank>

                         with chicken

2                       Regular Bacon ...       |  Sandwich

 

3                       Regular Turkey/Ham  | Sandwich 

 

- There are also ItemTypes such as salads, chips, cookies, add ons etc. but I am only testing sandwiches right now.

Example of what output dataset should look like:

 

ItemNumber     Description               ItemType        

 

1                       Double Flatbread     Sandwich

                         with chicken

etc.

 

Test Code (doesn't work):

 

DATA Work.TestData;

     SET Work.TestData;

           IF Item_Type = '' then DO;

                  IF FINDW(Description, 'Flatbread')>0 then Item_Type = 'Sandwich';

           END;

RUN;

 

The last forum I found the FINDW command was not very helpful in explaining the structure of it so I don't know if I even started doing it correctly.

 

Thank you for the help, if there is more code or data that you need to see I can get it.


 

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 725 views
  • 1 like
  • 2 in conversation