<?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 Dynammic macro variables to select data in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Dynammic-macro-variables-to-select-data/m-p/839864#M332051</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I need some help with my program.&lt;/P&gt;&lt;P&gt;I have a list of x therapies (thlist) in one variable. Now I want to create a macro variable (since thlist can change over time) that I can use in a dataset (data01) to exclude all data that has the therapy listet in thlist.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So far i got this:&lt;/P&gt;&lt;P&gt;*this is my therpylist;&lt;/P&gt;&lt;P&gt;data thlist;&lt;BR /&gt;set have;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;--&amp;gt; looks like this:&lt;/P&gt;&lt;P&gt;var therapy&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; th1&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; th2&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; th3&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;*Now create a macrolist;&lt;/P&gt;&lt;P&gt;data macrolist;&lt;BR /&gt;set thlist end=last;&lt;BR /&gt;call symput("norpt", therapy);&lt;BR /&gt;if last then call symput("numnorpt", _n_);&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;*%put total No of no reports: &amp;amp;numnorpt;&lt;BR /&gt;*%put no report Therapien: &amp;amp;norpt1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Following step doesnt work!&lt;/P&gt;&lt;P&gt;*replace hardcoding, creating macro;&lt;BR /&gt;options mtrace mprint;&lt;BR /&gt;%Macro norptlist;&lt;BR /&gt;data norpt_&amp;amp;&amp;amp;norpt&amp;amp;i;&lt;BR /&gt;set macrolist;&lt;BR /&gt;%do i =1 %to &amp;amp;numorigins;&lt;BR /&gt;where nr = "&amp;amp;&amp;amp;norpt&amp;amp;i";&lt;BR /&gt;%end;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;%Mend norptlist;&lt;BR /&gt;%norptlist;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I have the macrovariable I want to delete all data that have the therapy listed in my macrolist:&lt;/P&gt;&lt;P&gt;I would do this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DATA new;&lt;BR /&gt;SET data;&lt;BR /&gt;if therapy eq &amp;amp;&amp;amp;norpt&amp;amp;i then delete;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Would appreciate some help on this.&lt;/P&gt;&lt;P&gt;Thx&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 21 Oct 2022 08:18:15 GMT</pubDate>
    <dc:creator>drfzLee</dc:creator>
    <dc:date>2022-10-21T08:18:15Z</dc:date>
    <item>
      <title>Dynammic macro variables to select data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynammic-macro-variables-to-select-data/m-p/839864#M332051</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I need some help with my program.&lt;/P&gt;&lt;P&gt;I have a list of x therapies (thlist) in one variable. Now I want to create a macro variable (since thlist can change over time) that I can use in a dataset (data01) to exclude all data that has the therapy listet in thlist.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So far i got this:&lt;/P&gt;&lt;P&gt;*this is my therpylist;&lt;/P&gt;&lt;P&gt;data thlist;&lt;BR /&gt;set have;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;--&amp;gt; looks like this:&lt;/P&gt;&lt;P&gt;var therapy&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; th1&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; th2&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; th3&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;*Now create a macrolist;&lt;/P&gt;&lt;P&gt;data macrolist;&lt;BR /&gt;set thlist end=last;&lt;BR /&gt;call symput("norpt", therapy);&lt;BR /&gt;if last then call symput("numnorpt", _n_);&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;*%put total No of no reports: &amp;amp;numnorpt;&lt;BR /&gt;*%put no report Therapien: &amp;amp;norpt1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Following step doesnt work!&lt;/P&gt;&lt;P&gt;*replace hardcoding, creating macro;&lt;BR /&gt;options mtrace mprint;&lt;BR /&gt;%Macro norptlist;&lt;BR /&gt;data norpt_&amp;amp;&amp;amp;norpt&amp;amp;i;&lt;BR /&gt;set macrolist;&lt;BR /&gt;%do i =1 %to &amp;amp;numorigins;&lt;BR /&gt;where nr = "&amp;amp;&amp;amp;norpt&amp;amp;i";&lt;BR /&gt;%end;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;%Mend norptlist;&lt;BR /&gt;%norptlist;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I have the macrovariable I want to delete all data that have the therapy listed in my macrolist:&lt;/P&gt;&lt;P&gt;I would do this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DATA new;&lt;BR /&gt;SET data;&lt;BR /&gt;if therapy eq &amp;amp;&amp;amp;norpt&amp;amp;i then delete;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Would appreciate some help on this.&lt;/P&gt;&lt;P&gt;Thx&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Oct 2022 08:18:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynammic-macro-variables-to-select-data/m-p/839864#M332051</guid>
      <dc:creator>drfzLee</dc:creator>
      <dc:date>2022-10-21T08:18:15Z</dc:date>
    </item>
    <item>
      <title>Re: Dynammic macro variables to select data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynammic-macro-variables-to-select-data/m-p/839867#M332053</link>
      <description>&lt;P&gt;No macro coding needed.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
  select *
  from data
  where therapy not in (select therapy from thlist)
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If performance is of importance, use a data step hash object:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set data;
if _n_ = 1
then do;
  declare hash t (dataset:"thlist");
  t.definekey("therapy");
  t.definedone();
end;
if t.check() ne 0; /* not found */
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Untested, posted from my tablet.&lt;/P&gt;</description>
      <pubDate>Fri, 21 Oct 2022 08:40:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynammic-macro-variables-to-select-data/m-p/839867#M332053</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-10-21T08:40:31Z</dc:date>
    </item>
    <item>
      <title>Re: Dynammic macro variables to select data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynammic-macro-variables-to-select-data/m-p/839879#M332055</link>
      <description>&lt;P&gt;Just for completeness, the "classic" sort/data step method:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=thlist;
by therapy;
run;

proc sort data=data;
by therapy;
run;

data want;
merge
  data
  thlist (in=t)
;
by therapy;
if not t;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 21 Oct 2022 10:24:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynammic-macro-variables-to-select-data/m-p/839879#M332055</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-10-21T10:24:56Z</dc:date>
    </item>
    <item>
      <title>Re: Dynammic macro variables to select data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynammic-macro-variables-to-select-data/m-p/840801#M332445</link>
      <description>Thank you Kurt for your hints. I created a macro variable via proc sql. It works well for me.&lt;BR /&gt;&lt;BR /&gt;Proc sql;&lt;BR /&gt;select therapy into :threp separated by ' ' from have&lt;BR /&gt;quit;&lt;BR /&gt;%put &amp;amp;threp;&lt;BR /&gt;&lt;BR /&gt;DATA new;&lt;BR /&gt;SET data;&lt;BR /&gt;if therapy in (&amp;amp;threp.) then delete;&lt;BR /&gt;RUN;</description>
      <pubDate>Wed, 26 Oct 2022 10:56:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynammic-macro-variables-to-select-data/m-p/840801#M332445</guid>
      <dc:creator>drfzLee</dc:creator>
      <dc:date>2022-10-26T10:56:22Z</dc:date>
    </item>
    <item>
      <title>Re: Dynammic macro variables to select data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynammic-macro-variables-to-select-data/m-p/840860#M332472</link>
      <description>&lt;P&gt;This is a very brittle algorithm. Once your have dataset overflows the 64K limit of macro variables, it will fail.&lt;/P&gt;
&lt;P&gt;The data step MERGE method is immune to this (it will work until you run out of disk space, of which you probably have terabytes).&lt;/P&gt;
&lt;P&gt;Bottom line: don't use your method in anything meant for production use.&lt;/P&gt;</description>
      <pubDate>Wed, 26 Oct 2022 13:00:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynammic-macro-variables-to-select-data/m-p/840860#M332472</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-10-26T13:00:44Z</dc:date>
    </item>
  </channel>
</rss>

