BookmarkSubscribeRSS Feed
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 



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



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



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;




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 ;
    drop n;

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.

Check out the Boston Area SAS Users Group (BASUG) video archives:



Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

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.

Get the $99 certification deal.jpg



Back in the Classroom!

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

View all other training opportunities.

Discussion stats
  • 1 reply
  • 2 in conversation