<?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 Using SAS macro with substr in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-SAS-macro-with-substr/m-p/944539#M370073</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I think it's possible but am not sure how to do it. I have a CCW data with multiple ICD variables. The ICD variables are in this format&amp;nbsp;ICD_DGNS_CD1,&amp;nbsp;ICD_DGNS_CD2, and so on.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am writing a sql code to pull all OUD within the data using the ICD codes. In the where statement, I don't want to write the entire variables out (e.g.,&amp;nbsp;icd_dgns_cd1) in my code so am think there should be a macro code to call the ICD variables. I tried something like ; where&amp;nbsp;&amp;nbsp;(substr(icd_&amp;amp;,1,5) in (&amp;amp;icd.) but it didn't work.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;%let icd= '30400', '30401', '30402', '30403', '30470', '30471', '30472', '30473',...... ; &lt;BR /&gt;proc sql;
create table oud as
select id, clm_id, dob_dt, icd_dgns_cd1,&amp;nbsp;icd_dgns_cd2,&amp;nbsp;icd_dgns_cd3,&amp;nbsp;icd_dgns_cd4,
icd_dgns_cd5,&amp;nbsp;icd_dgns_cd6,&amp;nbsp;icd_dgns_cd7,&amp;nbsp;icd_dgns_cd8
from&amp;nbsp;bcarclms2021&amp;nbsp;
where&amp;nbsp;&amp;nbsp;(substr(icd_dgns_cd1,1,5) in (&amp;amp;icd.) or substr(icd_dgns_cd2,1,5) in (&amp;amp;icd.) or &amp;nbsp;&lt;BR /&gt;        substr(icd_dgns_cd3,1,5) in (&amp;amp;icd.) or substr(icd_dgns_cd4,1,5) in (&amp;amp;icd.) or
&amp;nbsp; &amp;nbsp; &amp;nbsp;   substr(icd_dgns_cd5,1,5) in (&amp;amp;icd.) or substr(icd_dgns_cd6,1,5) in (&amp;amp;icd.) or
&amp;nbsp; &amp;nbsp; &amp;nbsp;   substr(icd_dgns_cd7,1,5) in (&amp;amp;icd.) or substr(icd_dgns_cd8,1,5) in (&amp;amp;icd.) ; 
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 19 Sep 2024 02:09:21 GMT</pubDate>
    <dc:creator>CathyVI</dc:creator>
    <dc:date>2024-09-19T02:09:21Z</dc:date>
    <item>
      <title>Using SAS macro with substr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-SAS-macro-with-substr/m-p/944539#M370073</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I think it's possible but am not sure how to do it. I have a CCW data with multiple ICD variables. The ICD variables are in this format&amp;nbsp;ICD_DGNS_CD1,&amp;nbsp;ICD_DGNS_CD2, and so on.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am writing a sql code to pull all OUD within the data using the ICD codes. In the where statement, I don't want to write the entire variables out (e.g.,&amp;nbsp;icd_dgns_cd1) in my code so am think there should be a macro code to call the ICD variables. I tried something like ; where&amp;nbsp;&amp;nbsp;(substr(icd_&amp;amp;,1,5) in (&amp;amp;icd.) but it didn't work.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;%let icd= '30400', '30401', '30402', '30403', '30470', '30471', '30472', '30473',...... ; &lt;BR /&gt;proc sql;
create table oud as
select id, clm_id, dob_dt, icd_dgns_cd1,&amp;nbsp;icd_dgns_cd2,&amp;nbsp;icd_dgns_cd3,&amp;nbsp;icd_dgns_cd4,
icd_dgns_cd5,&amp;nbsp;icd_dgns_cd6,&amp;nbsp;icd_dgns_cd7,&amp;nbsp;icd_dgns_cd8
from&amp;nbsp;bcarclms2021&amp;nbsp;
where&amp;nbsp;&amp;nbsp;(substr(icd_dgns_cd1,1,5) in (&amp;amp;icd.) or substr(icd_dgns_cd2,1,5) in (&amp;amp;icd.) or &amp;nbsp;&lt;BR /&gt;        substr(icd_dgns_cd3,1,5) in (&amp;amp;icd.) or substr(icd_dgns_cd4,1,5) in (&amp;amp;icd.) or
&amp;nbsp; &amp;nbsp; &amp;nbsp;   substr(icd_dgns_cd5,1,5) in (&amp;amp;icd.) or substr(icd_dgns_cd6,1,5) in (&amp;amp;icd.) or
&amp;nbsp; &amp;nbsp; &amp;nbsp;   substr(icd_dgns_cd7,1,5) in (&amp;amp;icd.) or substr(icd_dgns_cd8,1,5) in (&amp;amp;icd.) ; 
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 19 Sep 2024 02:09:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-SAS-macro-with-substr/m-p/944539#M370073</guid>
      <dc:creator>CathyVI</dc:creator>
      <dc:date>2024-09-19T02:09:21Z</dc:date>
    </item>
    <item>
      <title>Re: Using SAS macro with substr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-SAS-macro-with-substr/m-p/944541#M370075</link>
      <description>&lt;P&gt;I don't see any attempt to use macro code posted.&amp;nbsp; Just normal SQL code.&amp;nbsp; Is that the SQL code you want to use macro code to create?&lt;/P&gt;
&lt;P&gt;So perhaps something like this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro query(indata,outdata,nvar,icdlist);
%local i var;
proc sql;
create table &amp;amp;outdata as
select id, clm_id, dob_dt
%do i=1 %to &amp;amp;nvar;
  %let var=icd_dgns_cd&amp;amp;i ;
  , &amp;amp;var
%end;
from &amp;amp;indata 
where 1=0
%do i=1 %to &amp;amp;nvar;
  %let var=icd_dgns_cd&amp;amp;i;
  or substr(&amp;amp;var,1,5) in (&amp;amp;icdlist)
%end;
;
quit;
%mend query;

%query
(indata=bcarclms2021
,outdata=oud
,nvars=8
,icdlist='30400' '30401' '30402' '30403' '30470' '30471' '30472' '30473'
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;NOTE that in SAS the IN operator does not care if you use spaces instead of commas between the items in the list.&amp;nbsp; Using spaces makes it much easier to deal with the list in macro code since commas might be confused as being part of the function call.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But why would you use SQL for this type of problem?&amp;nbsp; Why not just use normal SAS code?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let icdlist='30400' '30401' '30402' '30403' '30470' '30471' '30472' '30473';
data oud;
  set bcarclms2021;
  found=0;
  array icd_dgns_cd[8];
  do i=1 to dim(icd_dgns_cd) while(not found);
    found = substr(icd_dgns_cd[i],1,5) in (&amp;amp;icdlist);
  end;
  if found;
  drop found i;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 19 Sep 2024 03:01:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-SAS-macro-with-substr/m-p/944541#M370075</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-09-19T03:01:21Z</dc:date>
    </item>
    <item>
      <title>Re: Using SAS macro with substr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-SAS-macro-with-substr/m-p/944575#M370081</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/253321"&gt;@CathyVI&lt;/a&gt; what do you mean "it didn't work"? Saying "it didn't work" and providing no other information never is sufficient.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Did it generate an error? If so, show us the log for this PROC SQL. Are the results not what you expect? If so, explain a lot more so we can understand, and provide sample data.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Sep 2024 10:10:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-SAS-macro-with-substr/m-p/944575#M370081</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-09-19T10:10:01Z</dc:date>
    </item>
    <item>
      <title>Re: Using SAS macro with substr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-SAS-macro-with-substr/m-p/944577#M370082</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/253321"&gt;@CathyVI&lt;/a&gt;&amp;nbsp;From the looks of it the solution from &lt;A href="https://communities.sas.com/t5/SAS-Programming/Flagging-records-with-specific-values/m-p/944182#M369995" target="_self"&gt;this discussion&lt;/A&gt; could also be applicable to your case.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Sep 2024 10:39:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-SAS-macro-with-substr/m-p/944577#M370082</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-09-19T10:39:19Z</dc:date>
    </item>
  </channel>
</rss>

