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.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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