<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic How to extract observations containing a specific word + debugging in data step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-observations-containing-a-specific-word-debugging/m-p/885140#M349722</link>
    <description>&lt;P&gt;My goal is to extract all observations with an item_name that matches the value of the macro variable expense_type,&amp;nbsp; from a dataset like the one below&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="sadovsd_2-1689616346324.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/85857i994E31ED3B9A2EC7/image-size/medium?v=v2&amp;amp;px=400" role="button" title="sadovsd_2-1689616346324.png" alt="sadovsd_2-1689616346324.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Right now, my solution is only extracting those observations that start with that word:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="sadovsd_1-1689616315290.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/85856i097E687E11BBE260/image-size/medium?v=v2&amp;amp;px=400" role="button" title="sadovsd_1-1689616315290.png" alt="sadovsd_1-1689616315290.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;observations like the one below, which include that word in a different position are not included&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="sadovsd_0-1689616170665.png" style="width: 348px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/85855i08ABC057E5162860/image-dimensions/348x47?v=v2" width="348" height="47" role="button" title="sadovsd_0-1689616170665.png" alt="sadovsd_0-1689616170665.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've been trying to debug to figure out what the current word is in the loop iteration, but I keep getting errors.&lt;CODE class=""&gt;put 'current_word: ' scan(item_name, n);&lt;/CODE&gt; isn't working. How can I even debug in this case? this is my code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;/* find observations with item_codes corresponding to the specific expense type keyword user wants */
data item_docs_specific;
    set item_docs;
    %LET expense_type = "&amp;amp;expense_type";&lt;BR /&gt;    /* loop through item_name variable to find observations that match with the expense type keyword use.r wanted */&lt;BR /&gt;
    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:' &amp;amp;expense_type; */
    &lt;BR /&gt;        if count(lowcase(scan(item_name,n)), lowcase(&amp;amp;expense_type)) &amp;gt; 0;
        output;        
    end;&lt;BR /&gt;    drop n;&lt;BR /&gt;run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 17 Jul 2023 18:00:43 GMT</pubDate>
    <dc:creator>sadovsd</dc:creator>
    <dc:date>2023-07-17T18:00:43Z</dc:date>
    <item>
      <title>How to extract observations containing a specific word + debugging in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-observations-containing-a-specific-word-debugging/m-p/885140#M349722</link>
      <description>&lt;P&gt;My goal is to extract all observations with an item_name that matches the value of the macro variable expense_type,&amp;nbsp; from a dataset like the one below&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="sadovsd_2-1689616346324.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/85857i994E31ED3B9A2EC7/image-size/medium?v=v2&amp;amp;px=400" role="button" title="sadovsd_2-1689616346324.png" alt="sadovsd_2-1689616346324.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Right now, my solution is only extracting those observations that start with that word:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="sadovsd_1-1689616315290.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/85856i097E687E11BBE260/image-size/medium?v=v2&amp;amp;px=400" role="button" title="sadovsd_1-1689616315290.png" alt="sadovsd_1-1689616315290.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;observations like the one below, which include that word in a different position are not included&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="sadovsd_0-1689616170665.png" style="width: 348px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/85855i08ABC057E5162860/image-dimensions/348x47?v=v2" width="348" height="47" role="button" title="sadovsd_0-1689616170665.png" alt="sadovsd_0-1689616170665.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've been trying to debug to figure out what the current word is in the loop iteration, but I keep getting errors.&lt;CODE class=""&gt;put 'current_word: ' scan(item_name, n);&lt;/CODE&gt; isn't working. How can I even debug in this case? this is my code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;/* find observations with item_codes corresponding to the specific expense type keyword user wants */
data item_docs_specific;
    set item_docs;
    %LET expense_type = "&amp;amp;expense_type";&lt;BR /&gt;    /* loop through item_name variable to find observations that match with the expense type keyword use.r wanted */&lt;BR /&gt;
    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:' &amp;amp;expense_type; */
    &lt;BR /&gt;        if count(lowcase(scan(item_name,n)), lowcase(&amp;amp;expense_type)) &amp;gt; 0;
        output;        
    end;&lt;BR /&gt;    drop n;&lt;BR /&gt;run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 17 Jul 2023 18:00:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-observations-containing-a-specific-word-debugging/m-p/885140#M349722</guid>
      <dc:creator>sadovsd</dc:creator>
      <dc:date>2023-07-17T18:00:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract observations containing a specific word + debugging in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-observations-containing-a-specific-word-debugging/m-p/885145#M349723</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;To debug this, I would recommend running code like the following.&amp;nbsp; I created a variable WORD, for the nth wort of the string, and commented out your IF statement:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%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(&amp;amp;expense_type)) &amp;gt; 0 ;
        output;        
    end;
    drop n;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If that is working like you want, then uncomment the IF statement and see how it changes.&amp;nbsp; The IF statement here is your problem.&amp;nbsp; Note that it is a subsetting IF statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You don't have to loop through the list to do this.&amp;nbsp; You can use the FINDW function to search for a word like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data item_docs_specific;
  set item_docs;   
  if findw(item_name,&amp;amp;expense_type,' ',"i") ;
run ;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Mon, 17 Jul 2023 18:43:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-observations-containing-a-specific-word-debugging/m-p/885145#M349723</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-07-17T18:43:42Z</dc:date>
    </item>
  </channel>
</rss>

