<?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 Re: contains in if statement in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/contains-in-if-statement/m-p/901754#M356361</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I did case sensitive check and it its the same with upper and lower case.&lt;/P&gt;
&lt;P&gt;I wrote the code I mentioned using where-contains, but now because of the research question I need individuals who does not fall into those categories (&lt;SPAN&gt;("&lt;/SPAN&gt;&lt;SPAN&gt;ARIPIPRAZOLE&lt;/SPAN&gt;&lt;SPAN&gt;", "&lt;/SPAN&gt;&lt;SPAN&gt;ASENAPINE", "BREXPIPRAZOLE", etc.).&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I now want all individuals who has ("ARIPIPRAZOLE", "ASENAPINE", "BREXPIPRAZOLE", etc.). in &amp;nbsp;GENERIC_DRUG_NAME, to be named as drug=1 and who do not have, to be named drug=2. But its not working with the if then statement.So I am trying to use "contains" in if then statement.&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 06 Nov 2023 16:29:37 GMT</pubDate>
    <dc:creator>stellapersis7</dc:creator>
    <dc:date>2023-11-06T16:29:37Z</dc:date>
    <item>
      <title>contains in if statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/contains-in-if-statement/m-p/901669#M356327</link>
      <description>&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif" size="2"&gt;&lt;SPAN&gt;&lt;STRONG&gt;Hi all,&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif" size="2"&gt;I have used &amp;nbsp;the following where-contains statement in my program, but this is dropping other IDs which does not have GENERIC_DRUG_NAME. How can I use this same and also retain the other&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="arial, helvetica, sans-serif" size="2"&gt;IDs.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="arial, helvetica, sans-serif" size="2"&gt;Here is my code:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif" size="2"&gt;%MACRO HW2_RX(dsname);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif" size="2"&gt;DATA SGA_TREATMENT;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif" size="2"&gt;SET &lt;SPAN&gt;hw2J.&lt;/SPAN&gt;&amp;amp;dsname (KEEP= PERSON_ID GENERIC_DRUG_NAME);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif" size="2"&gt;WHERE (GENERIC_DRUG_NAME CONTAINS &lt;SPAN&gt;"ARIPIPRAZOLE"&lt;/SPAN&gt;)OR (GENERIC_DRUG_NAME CONTAINS &lt;SPAN&gt;"ASENAPINE"&lt;/SPAN&gt;)OR (GENERIC_DRUG_NAME CONTAINS &lt;SPAN&gt;"BREXPIPRAZOLE"&lt;/SPAN&gt;) OR (GENERIC_DRUG_NAME CONTAINS &lt;SPAN&gt;"CARIPRAZINE"&lt;/SPAN&gt;) OR&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif" size="2"&gt;(GENERIC_DRUG_NAME CONTAINS &lt;SPAN&gt;"CLOZAPINE"&lt;/SPAN&gt;) OR (GENERIC_DRUG_NAME CONTAINS &lt;SPAN&gt;"ILOPERIDONE"&lt;/SPAN&gt;) OR (GENERIC_DRUG_NAME CONTAINS &lt;SPAN&gt;"LURASIDONE"&lt;/SPAN&gt;) OR (GENERIC_DRUG_NAME CONTAINS &lt;SPAN&gt;"OLANZAPINE"&lt;/SPAN&gt;) OR&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif" size="2"&gt;(GENERIC_DRUG_NAME CONTAINS &lt;SPAN&gt;"PALIPERIDONE"&lt;/SPAN&gt;) OR (GENERIC_DRUG_NAME CONTAINS &lt;SPAN&gt;"PIMAVANSERIN"&lt;/SPAN&gt;) OR (GENERIC_DRUG_NAME CONTAINS &lt;SPAN&gt;"QUETIAPINE"&lt;/SPAN&gt;) OR (GENERIC_DRUG_NAME CONTAINS &lt;SPAN&gt;"RISPERIDONE"&lt;/SPAN&gt;) OR (GENERIC_DRUG_NAME CONTAINS &lt;SPAN&gt;"ZIPRASIDONE"&lt;/SPAN&gt;);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif" size="2"&gt;RUN;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif" size="2"&gt;Thank you&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Nov 2023 04:38:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/contains-in-if-statement/m-p/901669#M356327</guid>
      <dc:creator>stellapersis7</dc:creator>
      <dc:date>2023-11-06T04:38:19Z</dc:date>
    </item>
    <item>
      <title>Re: contains in if statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/contains-in-if-statement/m-p/901676#M356331</link>
      <description>&lt;P&gt;Do you want to also keep observations where generic_drug_name is missing/empty?&lt;/P&gt;</description>
      <pubDate>Mon, 06 Nov 2023 07:09:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/contains-in-if-statement/m-p/901676#M356331</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-11-06T07:09:23Z</dc:date>
    </item>
    <item>
      <title>Re: contains in if statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/contains-in-if-statement/m-p/901703#M356344</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for sharing the code, but I'm not sure I understand your requirement. The code indicates you only want observations that contain the quoted strings ("&lt;SPAN&gt;ARIPIPRAZOLE&lt;/SPAN&gt;", "&lt;SPAN&gt;ASENAPINE", "BREXPIPRAZOLE", etc.)&amp;nbsp;&lt;/SPAN&gt;in the variable&amp;nbsp;&lt;SPAN&gt;GENERIC_DRUG_NAME. If there are other observations that you also require then have you tried adding the conditions for them to your where clause?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;By default SAS performs case sensitive checks when comparing strings, so if your data is not in uppercase as per your code, then this will need to be fixed.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;It would help if you supplied some example input data (&lt;A href="https://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/" target="_blank" rel="noopener"&gt;ideally in the form of a data step&lt;/A&gt;) and what the corresponding output data should look like, with your reasons why you expect the data that is not appearing in your results.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks &amp;amp; kind regards,&lt;/P&gt;
&lt;P&gt;Amir.&lt;/P&gt;</description>
      <pubDate>Mon, 06 Nov 2023 12:13:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/contains-in-if-statement/m-p/901703#M356344</guid>
      <dc:creator>Amir</dc:creator>
      <dc:date>2023-11-06T12:13:50Z</dc:date>
    </item>
    <item>
      <title>Re: contains in if statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/contains-in-if-statement/m-p/901718#M356350</link>
      <description>&lt;P&gt;A further thought is if you want to see all the records for a person_id if any observation for that same person_id contains any of the quoted strings in&amp;nbsp;&lt;SPAN&gt;GENERIC_DRUG_NAME, then you can try something similar to the below. The code reads the built in sashelp.cars data set and brings back all observations for a make if any of the model values contain "X" or "Z". Just substitute make and model with your variables names, sashelp.cars with your input data set, and change the contains conditions to what you want.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  create table
    want
  as
  select
     make
    ,model
  from
    sashelp.cars
  where
    make in (select distinct
               make
             from
               sashelp.cars
             where
                  upcase(model) contains 'X'
               or upcase(model) contains 'Z'
            )
  ;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Thanks &amp;amp; kind regards,&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Amir.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Nov 2023 13:26:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/contains-in-if-statement/m-p/901718#M356350</guid>
      <dc:creator>Amir</dc:creator>
      <dc:date>2023-11-06T13:26:17Z</dc:date>
    </item>
    <item>
      <title>Re: contains in if statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/contains-in-if-statement/m-p/901736#M356357</link>
      <description>&lt;P&gt;Hello &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/435319"&gt;@stellapersis7&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;Your code as posted above will only list Person_id for records in which the where condition is true.&lt;BR /&gt;In case you want to have all records and also be able to identify the records that contain the required drug, then one approach is to create&amp;nbsp; a flag variable whose value will be 1 (integer one) in case there is a match. The data step code which you have posted would need to be replaces with something like this. Modify to suit your needs.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table SGA_TREATMENT as
SELECT  PERSON_ID, GENERIC_DRUG_NAME , 
CASE 
WHEN UPCAE(GENERIC_DRUG_NAME) in ("ARIPIPRAZOLE"."ASENAPINE", "BREXPIPRAZOLE", "CARIPRAZINE"), "CLOZAPINE", "ILOPERIDONE" , "LURASIDONE",  "OLANZAPINE", "PALIPERIDONE", "PIMAVANSERIN", "QUETIAPINE",  "RISPERIDONE", "ZIPRASIDONE")
THEN 1
END AS FLAG
FROM hw2J.&amp;amp;dsname;&lt;BR /&gt;quit;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note : I haven't tested the code. Don't have your source.&lt;BR /&gt;Another approach would be to create a dataset withe the names of the above drugs and create a full outer join.&lt;/P&gt;</description>
      <pubDate>Mon, 06 Nov 2023 14:48:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/contains-in-if-statement/m-p/901736#M356357</guid>
      <dc:creator>Sajid01</dc:creator>
      <dc:date>2023-11-06T14:48:05Z</dc:date>
    </item>
    <item>
      <title>Re: contains in if statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/contains-in-if-statement/m-p/901751#M356360</link>
      <description>&lt;P&gt;no, I can drop them.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Mon, 06 Nov 2023 16:21:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/contains-in-if-statement/m-p/901751#M356360</guid>
      <dc:creator>stellapersis7</dc:creator>
      <dc:date>2023-11-06T16:21:54Z</dc:date>
    </item>
    <item>
      <title>Re: contains in if statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/contains-in-if-statement/m-p/901754#M356361</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I did case sensitive check and it its the same with upper and lower case.&lt;/P&gt;
&lt;P&gt;I wrote the code I mentioned using where-contains, but now because of the research question I need individuals who does not fall into those categories (&lt;SPAN&gt;("&lt;/SPAN&gt;&lt;SPAN&gt;ARIPIPRAZOLE&lt;/SPAN&gt;&lt;SPAN&gt;", "&lt;/SPAN&gt;&lt;SPAN&gt;ASENAPINE", "BREXPIPRAZOLE", etc.).&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I now want all individuals who has ("ARIPIPRAZOLE", "ASENAPINE", "BREXPIPRAZOLE", etc.). in &amp;nbsp;GENERIC_DRUG_NAME, to be named as drug=1 and who do not have, to be named drug=2. But its not working with the if then statement.So I am trying to use "contains" in if then statement.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Nov 2023 16:29:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/contains-in-if-statement/m-p/901754#M356361</guid>
      <dc:creator>stellapersis7</dc:creator>
      <dc:date>2023-11-06T16:29:37Z</dc:date>
    </item>
    <item>
      <title>Re: contains in if statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/contains-in-if-statement/m-p/901755#M356362</link>
      <description>&lt;P&gt;You cannot use CONTAINS in a IF statement.&amp;nbsp; That is an SQL concept and so only works in WHERE statement.&lt;/P&gt;
&lt;PRE&gt;565  data test;
566   set sashelp.class;
567   if name contains 'a' then put name=;
              --------
              388
              76
ERROR 388-185: Expecting an arithmetic operator.

ERROR 76-322: Syntax error, statement will be ignored.

568  run;
&lt;/PRE&gt;
&lt;P&gt;Use INDEX or INDEXW or FIND or FINDW depending on what you are doing.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
   if index(upcase(generic_name),'ARIPRAZOLE') then drug=1;
   else if index(upcase(generic_name), ....
   else if index....
   else drug=2;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 06 Nov 2023 16:35:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/contains-in-if-statement/m-p/901755#M356362</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-11-06T16:35:21Z</dc:date>
    </item>
    <item>
      <title>Re: contains in if statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/contains-in-if-statement/m-p/901756#M356363</link>
      <description>&lt;P&gt;Silly question: Why is your subject line "Contains in if statement"?&lt;/P&gt;
&lt;P&gt;You do not show an IF statement or condition anywhere in the example code?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Where and If are different statements. Where applies to the incoming data vector and If to&amp;nbsp; the working data vector after the data is read. There are also restrictions as to things like "contains" and "like" with Where but not available with If.&lt;/P&gt;</description>
      <pubDate>Mon, 06 Nov 2023 16:35:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/contains-in-if-statement/m-p/901756#M356363</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-11-06T16:35:41Z</dc:date>
    </item>
    <item>
      <title>Re: contains in if statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/contains-in-if-statement/m-p/901757#M356364</link>
      <description>&lt;P&gt;If your data are sorted by PERSON_ID and you want to keep all obs for any person_id that has at least one qualifying obs, then you can (1) change your SET to a MERGE, (2) change the WHERE statement to a dataset-name-option, and (3) add a BY statement and a subsetting IF statement:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  merge hw2J.&amp;amp;dsname
   (KEEP= PERSON_ID GENERIC_DRUG_NAME
    where=( (GENERIC_DRUG_NAME CONTAINS "ARIPIPRAZOLE")
           OR GENERIC_DRUG_NAME CONTAINS "ASENAPINE")
           OR (GENERIC_DRUG_NAME CONTAINS "BREXPIPRAZOLE") 
           OR (GENERIC_DRUG_NAME CONTAINS "CARIPRAZINE") 
           OR (GENERIC_DRUG_NAME CONTAINS "CLOZAPINE") 
           OR (GENERIC_DRUG_NAME CONTAINS "ILOPERIDONE") 
           OR (GENERIC_DRUG_NAME CONTAINS "LURASIDONE") 
           OR (GENERIC_DRUG_NAME CONTAINS "OLANZAPINE") 
           OR (GENERIC_DRUG_NAME CONTAINS "PALIPERIDONE") 
           OR (GENERIC_DRUG_NAME CONTAINS "PIMAVANSERIN") 
           OR (GENERIC_DRUG_NAME CONTAINS "QUETIAPINE") 
           OR (GENERIC_DRUG_NAME CONTAINS "RISPERIDONE") 
           OR (GENERIC_DRUG_NAME CONTAINS "ZIPRASIDONE")
         )
       in=flag)
   hw2J.&amp;amp;dsname (KEEP= PERSON_ID GENERIC_DRUG_NAME)  ;
  by person_id;
  if flag=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 06 Nov 2023 16:40:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/contains-in-if-statement/m-p/901757#M356364</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-11-06T16:40:03Z</dc:date>
    </item>
    <item>
      <title>Re: contains in if statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/contains-in-if-statement/m-p/901826#M356381</link>
      <description>&lt;P&gt;Below one coding option for testing of existence of a list of substrings in a string.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines truncover dsd;
  input GENERIC_DRUG_NAME $100.;
  person_id+1;
  datalines;
ARIPIPRAZOLE
PIMAVANSERIN
xxxPIMAVANSERIN
xxxAAAxxx
xxxQUETIAPINExxx
 
PIMAVANSERINxxx
;

data want(drop=_:);
  set have (keep= person_id generic_drug_name);
  array drug{3} $15 _temporary_ ('ARIPIPRAZOLE','PIMAVANSERIN','QUETIAPINE');
  do _i=1 to dim(drug);
    if find(generic_drug_name,drug[_i],'it') then 
      do;
        output;
        leave;
      end;
  end;
run;

proc print data=want;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;If you've got a table with the drug names you want to select then you could also generate the array statement dynamically. Example below:&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines truncover dsd;
  input GENERIC_DRUG_NAME $100.;
  person_id+1;
  datalines;
ARIPIPRAZOLE
PIMAVANSERIN
xxxPIMAVANSERIN
xxxAAAxxx
xxxQUETIAPINExxx
 
PIMAVANSERINxxx
;

data drugs;
  infile datalines truncover dsd;
  input drug $20.;
  datalines;
ARIPIPRAZOLE
ASENAPINE
BREXPIPRAZOLE
CARIPRAZINE
CLOZAPINE
ILOPERIDONE
LURASIDONE
OLANZAPINE
PALIPERIDONE
PIMAVANSERIN
QUETIAPINE
RISPERIDONE
ZIPRASIDONE
;

proc sql noprint;
  select 
    cats("'",drug,"'"), 
    count(*),
    max(length(drug))
    into
      :drug_list separated by ',',
      :n_drugs trimmed,
      :max_len trimmed
  from drugs
  ;
quit;

data want(drop=_:);
  set have (keep= person_id generic_drug_name);
  array drug{&amp;amp;n_drugs} $&amp;amp;max_len _temporary_ (&amp;amp;drug_list);
  do _i=1 to dim(drug);
    if find(generic_drug_name,drug[_i],'it') then 
      do;
        output;
        leave;
      end;
  end;
run;

proc print data=want;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI-SPOILER&gt;</description>
      <pubDate>Mon, 06 Nov 2023 23:43:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/contains-in-if-statement/m-p/901826#M356381</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-11-06T23:43:08Z</dc:date>
    </item>
  </channel>
</rss>

