<?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 check series of conditional statements to assign values based on keywords in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/check-series-of-conditional-statements-to-assign-values-based-on/m-p/950452#M371685</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope you are doing well!&lt;/P&gt;&lt;P&gt;I have variable called mdotsp, contains values of missing number of tablets but it has different keywords such as "pill", "TAB", etc. I am trying to create numeric variable called mnmtb to assign a value from mdotsp which contains a number of missing pills.&lt;/P&gt;&lt;P&gt;I am using below code to check series of conditional statements based on keywords. I have large data but here I have provided some example data. In below I am getting mnmtb value as missing. I am not sure why, can anyone help me with this logic where I am missing?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data have;
input mdotsp $50.;
datalines;
1PILL
2TAB
3MINI
4PIECE
#5
5#
;
run;

data want;
    length mnmtb 8;
    set have;
    mdotsp = compress(upcase(mdotsp)); /* Normalize mdotsp for case and spaces */

    /* Loop through 1 to 300 for mnmtb */
    do n = 5 to 1 by -1;
        /* Create potential keywords dynamically */
        pill_keyword = cats(n, "PILL");
        one_pill_keyword = cats(scan(put(n, best.), 1), "PILL");
        tab_keyword = cats(n, "TAB");
        one_tab_keyword = cats(scan(put(n, best.), 1), "TAB");
        mini_keyword = cats(n, "MINI");
        one_mini_keyword = cats(scan(put(n, best.), 1), "MINI");
        piece_keyword = cats(n, "PIECE");
        one_piece_keyword = cats(scan(put(n, best.), 1), "PIECE");
        hash_keyword = cats(n, "#");
        hash_rev_keyword = cats("#", n);

        /* Check if any of these keywords are present in mdotsp */
        if index(mdotsp, pill_keyword) or index(mdotsp, one_pill_keyword) or 
           index(mdotsp, tab_keyword) or index(mdotsp, one_tab_keyword) or 
           index(mdotsp, mini_keyword) or index(mdotsp, one_mini_keyword) or 
           index(mdotsp, piece_keyword) or index(mdotsp, one_piece_keyword) or 
           index(mdotsp, hash_keyword) or index(mdotsp, hash_rev_keyword) then do;
           mnmtb=n;
           leave; /* Exit the loop once a match is found */
        end;
    end;

run;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;chin&lt;/P&gt;</description>
    <pubDate>Tue, 12 Nov 2024 16:32:26 GMT</pubDate>
    <dc:creator>chinna0369</dc:creator>
    <dc:date>2024-11-12T16:32:26Z</dc:date>
    <item>
      <title>check series of conditional statements to assign values based on keywords</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-series-of-conditional-statements-to-assign-values-based-on/m-p/950452#M371685</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope you are doing well!&lt;/P&gt;&lt;P&gt;I have variable called mdotsp, contains values of missing number of tablets but it has different keywords such as "pill", "TAB", etc. I am trying to create numeric variable called mnmtb to assign a value from mdotsp which contains a number of missing pills.&lt;/P&gt;&lt;P&gt;I am using below code to check series of conditional statements based on keywords. I have large data but here I have provided some example data. In below I am getting mnmtb value as missing. I am not sure why, can anyone help me with this logic where I am missing?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data have;
input mdotsp $50.;
datalines;
1PILL
2TAB
3MINI
4PIECE
#5
5#
;
run;

data want;
    length mnmtb 8;
    set have;
    mdotsp = compress(upcase(mdotsp)); /* Normalize mdotsp for case and spaces */

    /* Loop through 1 to 300 for mnmtb */
    do n = 5 to 1 by -1;
        /* Create potential keywords dynamically */
        pill_keyword = cats(n, "PILL");
        one_pill_keyword = cats(scan(put(n, best.), 1), "PILL");
        tab_keyword = cats(n, "TAB");
        one_tab_keyword = cats(scan(put(n, best.), 1), "TAB");
        mini_keyword = cats(n, "MINI");
        one_mini_keyword = cats(scan(put(n, best.), 1), "MINI");
        piece_keyword = cats(n, "PIECE");
        one_piece_keyword = cats(scan(put(n, best.), 1), "PIECE");
        hash_keyword = cats(n, "#");
        hash_rev_keyword = cats("#", n);

        /* Check if any of these keywords are present in mdotsp */
        if index(mdotsp, pill_keyword) or index(mdotsp, one_pill_keyword) or 
           index(mdotsp, tab_keyword) or index(mdotsp, one_tab_keyword) or 
           index(mdotsp, mini_keyword) or index(mdotsp, one_mini_keyword) or 
           index(mdotsp, piece_keyword) or index(mdotsp, one_piece_keyword) or 
           index(mdotsp, hash_keyword) or index(mdotsp, hash_rev_keyword) then do;
           mnmtb=n;
           leave; /* Exit the loop once a match is found */
        end;
    end;

run;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;chin&lt;/P&gt;</description>
      <pubDate>Tue, 12 Nov 2024 16:32:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-series-of-conditional-statements-to-assign-values-based-on/m-p/950452#M371685</guid>
      <dc:creator>chinna0369</dc:creator>
      <dc:date>2024-11-12T16:32:26Z</dc:date>
    </item>
    <item>
      <title>Re: check series of conditional statements to assign values based on keywords</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-series-of-conditional-statements-to-assign-values-based-on/m-p/950457#M371686</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;        /* Check if any of these keywords are present in mdotsp */
        if mnmtb ne . and (index(mdotsp, pill_keyword) or index(mdotsp, one_pill_keyword) or &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;MNMTB has not yet been assigned a value, so when you test to see if it is not equal to missing , it is equal to missing and so this IF condition is always false and the rest of the DO; END; never executes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It always helps, and it would help here tremendously, if you explain what you are trying to do in words, explain the purpose of this code. Do not discuss code or SAS, we need to know the purpose.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Nov 2024 16:11:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-series-of-conditional-statements-to-assign-values-based-on/m-p/950457#M371686</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-11-12T16:11:52Z</dc:date>
    </item>
    <item>
      <title>Re: check series of conditional statements to assign values based on keywords</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-series-of-conditional-statements-to-assign-values-based-on/m-p/950461#M371688</link>
      <description>&lt;P&gt;First problem: you only assign a value when the value is not missing:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;if mnmtb ne . and &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If your intent is to assign a value when MNMTB is missing then&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;if missing (mnmtb) and &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Second is when you use a variable as the second parameter of INDEX (and many other character functions) then entire variable is used. Since&amp;nbsp; you have not defined a length for any of your key word variables they are defaulting to 200 because of the CATS function. So the trailing spaces in the variable are used in the comparison and not found. So all of the index function calls are returning 0.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If N is never two digits you could get away with variable lengths to prevent that. OR remove the trailing spaces.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A drastically shortened example that returns a 1 for the 1PILL:&lt;/P&gt;
&lt;PRE&gt;data want;
   length mnmtb 8;
   set have;
   mdotsp = compress(upcase(mdotsp)); /* Normalize mdotsp for case and spaces */
   pill_keyword = cats(1, "PILL");
   /* Check if any of these keywords are present in mdotsp */
   if missing (mnmtb) and index(mdotsp, strip(pill_keyword))  then do;
      mnmtb=1;
   end;

run;&lt;/PRE&gt;</description>
      <pubDate>Tue, 12 Nov 2024 16:21:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-series-of-conditional-statements-to-assign-values-based-on/m-p/950461#M371688</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-11-12T16:21:37Z</dc:date>
    </item>
    <item>
      <title>Re: check series of conditional statements to assign values based on keywords</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-series-of-conditional-statements-to-assign-values-based-on/m-p/950463#M371690</link>
      <description>Edited my message.</description>
      <pubDate>Tue, 12 Nov 2024 16:32:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-series-of-conditional-statements-to-assign-values-based-on/m-p/950463#M371690</guid>
      <dc:creator>chinna0369</dc:creator>
      <dc:date>2024-11-12T16:32:52Z</dc:date>
    </item>
    <item>
      <title>Re: check series of conditional statements to assign values based on keywords</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-series-of-conditional-statements-to-assign-values-based-on/m-p/950464#M371691</link>
      <description>What about other keywords and I have mdotsp values from 1 to 300.</description>
      <pubDate>Tue, 12 Nov 2024 16:34:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-series-of-conditional-statements-to-assign-values-based-on/m-p/950464#M371691</guid>
      <dc:creator>chinna0369</dc:creator>
      <dc:date>2024-11-12T16:34:28Z</dc:date>
    </item>
    <item>
      <title>Re: check series of conditional statements to assign values based on keywords</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-series-of-conditional-statements-to-assign-values-based-on/m-p/950466#M371693</link>
      <description>&lt;P&gt;Use the STRIP function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;IF your next steps don't really need all those composite "keywords" I would drop them from the data set as that is a lot of baggage of very little use that I can see.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or perhaps research REGULAR expressions which can search for things like &amp;lt;digits&amp;gt;WORD values and return the Digit part if done correctly.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Nov 2024 16:53:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-series-of-conditional-statements-to-assign-values-based-on/m-p/950466#M371693</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-11-12T16:53:23Z</dc:date>
    </item>
    <item>
      <title>Re: check series of conditional statements to assign values based on keywords</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-series-of-conditional-statements-to-assign-values-based-on/m-p/950487#M371694</link>
      <description>I have updated my code as below:&lt;BR /&gt;data step3;&lt;BR /&gt;set step2;&lt;BR /&gt;if .&amp;lt; length(mdotsp) le 3 and index(mdotsp,"#") eq 0 then do; mnmtb=input(mdotsp,best.); end;&lt;BR /&gt;if strip(mdotsp)="1 TBALET WAS MISSED TO TAKE, REASON WAS UNKNOWN." then mnmtb=1;&lt;BR /&gt;mdotsp_comp=compress(upcase(mdotsp)); /* Normalize mdotsp for case and spaces */&lt;BR /&gt;do n = 300 to 1 by -1;&lt;BR /&gt;description=upcase(put(n,words100.));&lt;BR /&gt;pill_keyword = cats(n, "PILL");&lt;BR /&gt;word_pill_keyword = cats(description, "PILL");&lt;BR /&gt;tab_keyword = cats(n, "TAB");&lt;BR /&gt;word_tab_keyword = cats(description, "TAB");&lt;BR /&gt;mini_keyword = cats(n, "MINI");&lt;BR /&gt;word_mini_keyword = cats(description, "MINI");&lt;BR /&gt;piece_keyword = cats(n, "PIECE");&lt;BR /&gt;word_piece_keyword = cats(description, "PIECE");&lt;BR /&gt;hash_keyword = cats(n, "#");&lt;BR /&gt;word_hash_keyword = cats(description, "#");&lt;BR /&gt;hash_rev_keyword = cats("#", n);&lt;BR /&gt;word_hash_rev_keyword = cats("#", description);&lt;BR /&gt;slice_keyword = cats(n, "SLICE");&lt;BR /&gt;word_slice_keyword = cats(description, "SLICE");&lt;BR /&gt;stdydrg_keyword = cats(n, "STUDYDRUG");&lt;BR /&gt;word_stdydrg_keyword = cats(description, "STUDYDRUG");&lt;BR /&gt;keyword_missed = cats("MISSED",n);&lt;BR /&gt;word_keyword_missed = cats("MISSED",description);&lt;BR /&gt;caps_keyword = cats(n, "CAPSULE");&lt;BR /&gt;word_caps_keyword = cats(description, "CAPSULE");&lt;BR /&gt;/* Check if any of these keywords are present in mdotsp_comp */&lt;BR /&gt;if missing (mnmtb) and (&lt;BR /&gt;index(mdotsp_comp, strip(pill_keyword)) or index(mdotsp_comp, strip(word_pill_keyword)) or&lt;BR /&gt;index(mdotsp_comp, strip(tab_keyword)) or index(mdotsp_comp, strip(word_tab_keyword)) or&lt;BR /&gt;index(mdotsp_comp, strip(mini_keyword)) or index(mdotsp_comp, strip(word_mini_keyword)) or&lt;BR /&gt;index(mdotsp_comp, strip(piece_keyword)) or index(mdotsp_comp, strip(word_piece_keyword)) or&lt;BR /&gt;index(mdotsp_comp, strip(hash_keyword)) or index(mdotsp_comp, strip(word_hash_keyword)) or&lt;BR /&gt;index(mdotsp_comp, strip(hash_rev_keyword)) or index(mdotsp_comp, strip(word_hash_rev_keyword)) or&lt;BR /&gt;index(mdotsp_comp, strip(slice_keyword)) or index(mdotsp_comp, strip(word_slice_keyword)) or&lt;BR /&gt;index(mdotsp_comp, strip(stdydrg_keyword)) or index(mdotsp_comp, strip(word_stdydrg_keyword)) or&lt;BR /&gt;index(mdotsp_comp, strip(keyword_missed)) or index(mdotsp_comp, strip(word_keyword_missed)) or&lt;BR /&gt;index(mdotsp_comp, strip(caps_keyword)) or index(mdotsp_comp, strip(word_caps_keyword))&lt;BR /&gt;) then do;&lt;BR /&gt;mnmtb=n;&lt;BR /&gt;end;&lt;BR /&gt;end;&lt;BR /&gt;run;</description>
      <pubDate>Tue, 12 Nov 2024 18:50:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-series-of-conditional-statements-to-assign-values-based-on/m-p/950487#M371694</guid>
      <dc:creator>chinna0369</dc:creator>
      <dc:date>2024-11-12T18:50:26Z</dc:date>
    </item>
    <item>
      <title>Re: check series of conditional statements to assign values based on keywords</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-series-of-conditional-statements-to-assign-values-based-on/m-p/950488#M371695</link>
      <description>&lt;P&gt;I think you can just use the magic of regular expressions for this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
n=prxchange('s/.*?(\d+).*/\1/', 1, mdotsp)*1;
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 12 Nov 2024 19:04:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-series-of-conditional-statements-to-assign-values-based-on/m-p/950488#M371695</guid>
      <dc:creator>quickbluefish</dc:creator>
      <dc:date>2024-11-12T19:04:50Z</dc:date>
    </item>
  </channel>
</rss>

