BookmarkSubscribeRSS Feed
sadovsd
Calcite | Level 5

My goal is to extract all observations with an item_name that matches the value of the macro variable expense_type,  from a dataset like the one below 

sadovsd_2-1689616346324.png

 

Right now, my solution is only extracting those observations that start with that word:

sadovsd_1-1689616315290.png

 

observations like the one below, which include that word in a different position are not included

sadovsd_0-1689616170665.png

 

I've been trying to debug to figure out what the current word is in the loop iteration, but I keep getting errors.put 'current_word: ' scan(item_name, n); isn't working. How can I even debug in this case? this is my code:

 

/* find observations with item_codes corresponding to the specific expense type keyword user wants */
data item_docs_specific;
    set item_docs;
    %LET expense_type = "&expense_type";
/* loop through item_name variable to find observations that match with the expense type keyword use.r wanted */
do n=1 to countw(item_name); put 'item_name:' item_name; /* put 'current word #': n; */ /* put 'current_word: ' scan(item_name, n); */ /* put 'expense_type:' &expense_type; */
if count(lowcase(scan(item_name,n)), lowcase(&expense_type)) > 0; output; end;
drop n;
run;

 

1 REPLY 1
Quentin
Super User

Hi,


To debug this, I would recommend running code like the following.  I created a variable WORD, for the nth wort of the string, and commented out your IF statement:

 

%LET expense_type = "Care";
data item_docs ;
  input item_name  $16. ;
  cards ;
Care of invalids
Something else
Personal care
run ;


data item_docs_specific;
    set item_docs;   
    /* loop through item_name variable to find observations that match with the expense type keyword use.r wanted */

    do n=1 to countw(item_name);		
        word=scan(item_name, n) ;
        put item_name= n= word= ;
   
        *** if count(lowcase(scan(item_name,n)), lowcase(&expense_type)) > 0 ;
        output;        
    end;
    drop n;
run;

If that is working like you want, then uncomment the IF statement and see how it changes.  The IF statement here is your problem.  Note that it is a subsetting IF statement.

 

You don't have to loop through the list to do this.  You can use the FINDW function to search for a word like:

data item_docs_specific;
  set item_docs;   
  if findw(item_name,&expense_type,' ',"i") ;
run ;

But since you've already written the loop, it's worth debugging your current code, just to think through debugging a loop, and the implications of using a subsetting IF statement inside a loop.

The Boston Area SAS Users Group (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 465 views
  • 0 likes
  • 2 in conversation