<?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: How to dynamically change &amp;quot;%put list &amp;quot; in Macro depending on the dataset. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-dynamically-change-quot-put-list-quot-in-Macro-depending/m-p/794116#M254602</link>
    <description>&lt;P&gt;I'm guessing you might have created these macro variables as global macro variables earlier.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%put _user_ ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That will show all user-created macro variables (global and local). Generally, it's better to create local macro variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another option, if you want to print the value of all macro variables that start with DRUG, is to use the dictionary table/view that stores values of macro variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set sashelp.vmacro;
  where name like "DRUG%" ;  
  put name value ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 02 Feb 2022 21:39:56 GMT</pubDate>
    <dc:creator>Quentin</dc:creator>
    <dc:date>2022-02-02T21:39:56Z</dc:date>
    <item>
      <title>How to dynamically change "%put list " in Macro depending on the dataset.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-dynamically-change-quot-put-list-quot-in-Macro-depending/m-p/794087#M254590</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a requirement when I am creating a macro variables suing %total macro below. However , how I can&amp;nbsp; change the %put list in the macro dynamically( red font in code).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example in my 'x' data I have 4 observations&amp;nbsp; but in my next dataset 'Y' I have 6. So How I can change it dynamically depending on the dataset.&amp;nbsp; I appreciate your time and inputs. Thanks.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data x;
input drug count;
cards;
1 2
2 35
3 43
4 26
; 
run;

data y;
input drug count;
cards;
1 2
2 35
3 43
4 26
7 45
5 28
; 
run;
%macro total (in=);
data _null_;
     set &amp;amp;in.;
if count=. then count=0;
call symput ('drug'||trim(left(put(drug,best.))),trim(left(put(count,best.))));
run;

&lt;FONT color="#FF0000"&gt;%put &amp;amp;drug1 &amp;amp;drug2 &amp;amp;drug3 &amp;amp;drug4 ;&lt;/FONT&gt;
%mend;

%total (in= x);&lt;BR /&gt;%total (in= y);

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 02 Feb 2022 19:31:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-dynamically-change-quot-put-list-quot-in-Macro-depending/m-p/794087#M254590</guid>
      <dc:creator>SASuserlot</dc:creator>
      <dc:date>2022-02-02T19:31:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to dynamically change "%put list " in Macro depending on the dataset.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-dynamically-change-quot-put-list-quot-in-Macro-depending/m-p/794091#M254592</link>
      <description>&lt;P&gt;Why not just put the values directly in the Data Step with the Put Statement?&lt;/P&gt;</description>
      <pubDate>Wed, 02 Feb 2022 20:01:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-dynamically-change-quot-put-list-quot-in-Macro-depending/m-p/794091#M254592</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-02-02T20:01:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to dynamically change "%put list " in Macro depending on the dataset.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-dynamically-change-quot-put-list-quot-in-Macro-depending/m-p/794094#M254594</link>
      <description>&lt;P&gt;Why?&lt;/P&gt;
&lt;P&gt;A really don't see the utility here.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A somewhat different approach, assuming you really need those macro variables at all:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%macro total (in=);
data _null_;
     set &amp;amp;in.;
if count=. then count=0;
call symput ('drug'||trim(left(put(drug,best.))),trim(left(put(count,best.))));
run;

proc sql noprint;
   select drug into: druglist separated by ' '
   from &amp;amp;in.;
quit;

%put &amp;amp;druglist. ;
%mend;&lt;/PRE&gt;
&lt;P&gt;The sql code creates a single macro variable that should have the contents of the drug macro variables and %put that variable.&lt;/P&gt;</description>
      <pubDate>Wed, 02 Feb 2022 20:19:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-dynamically-change-quot-put-list-quot-in-Macro-depending/m-p/794094#M254594</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-02-02T20:19:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to dynamically change "%put list " in Macro depending on the dataset.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-dynamically-change-quot-put-list-quot-in-Macro-depending/m-p/794095#M254595</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data x;
input drug count;
cards;
1 2
2 35
3 43
4 26
; 
run;

data y;
input drug count;
cards;
1 2
2 35
3 43
4 26
7 45
5 28
; 
run;
%macro total (in=);
data _null_;
     set &amp;amp;in. end=eof;

call symputx (catt('drug', put(drug,best. -l)) , coalesce(count, 0), 'g');

if eof then call symputx('num_drugs', _n_);
run;

%do i=1 %to &amp;amp;num_drugs;
%put &amp;amp;&amp;amp;&amp;amp;drug&amp;amp;i.;
%end;

%mend;

%total (in= x);
%total (in= y);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here's a slightly cleaned up version of your code, &lt;STRONG&gt;but for your Y data set your drugs are numbered non-sequentially, ie drug7, no drug6 but 6 drugs. Do you want to be able to access those values dynamically?&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If so, perhaps use two macro variables, one to contain variable name and one value but index with rows?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro total (in=);
data _null_;
     set &amp;amp;in. end=eof;

*drug name;
call symputx(catt('drug_name', _n_), drug, 'g');

*drug count;
call symputx(catt('drug_count', _n_) , coalesce(count, 0), 'g');

if eof then call symputx('num_drugs', _n_);
run;

%do i=1 %to &amp;amp;num_drugs;
%put Drug Name : &amp;amp;&amp;amp;&amp;amp;drug_name&amp;amp;i.;
%put Drug Count: &amp;amp;&amp;amp;&amp;amp;drug_count&amp;amp;i.;
%end;

%mend;

%total (in= x);
%total (in= y);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However, I suspect this is all possibly a bad design and CALL EXECUTE and data driven approach may be better.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/350312"&gt;@SASuserlot&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a requirement when I am creating a macro variables suing %total macro below. However , how I can&amp;nbsp; change the %put list in the macro dynamically( red font in code).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example in my 'x' data I have 4 observations&amp;nbsp; but in my next dataset 'Y' I have 6. So How I can change it dynamically depending on the dataset.&amp;nbsp; I appreciate your time and inputs. Thanks.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data x;
input drug count;
cards;
1 2
2 35
3 43
4 26
; 
run;

data y;
input drug count;
cards;
1 2
2 35
3 43
4 26
7 45
5 28
; 
run;
%macro total (in=);
data _null_;
     set &amp;amp;in.;
if count=. then count=0;
call symput ('drug'||trim(left(put(drug,best.))),trim(left(put(count,best.))));
run;

&lt;FONT color="#FF0000"&gt;%put &amp;amp;drug1 &amp;amp;drug2 &amp;amp;drug3 &amp;amp;drug4 ;&lt;/FONT&gt;
%mend;

%total (in= x);&lt;BR /&gt;%total (in= y);

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Feb 2022 21:05:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-dynamically-change-quot-put-list-quot-in-Macro-depending/m-p/794095#M254595</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-02-02T21:05:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to dynamically change "%put list " in Macro depending on the dataset.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-dynamically-change-quot-put-list-quot-in-Macro-depending/m-p/794108#M254598</link>
      <description>&lt;P&gt;I do understand u r question . Its actually part of bigger macro where the datasets created running through the proc freq and other procedure. I just given the sample of dataset how it may look at the end before creating the macro variables. I may have to run the step before the macro creation for multiple times.. %put&amp;nbsp; will give me peace of mind when I look through the log to check my number matched for validation instead of opening the dataset.&lt;/P&gt;</description>
      <pubDate>Wed, 02 Feb 2022 20:58:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-dynamically-change-quot-put-list-quot-in-Macro-depending/m-p/794108#M254598</guid>
      <dc:creator>SASuserlot</dc:creator>
      <dc:date>2022-02-02T20:58:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to dynamically change "%put list " in Macro depending on the dataset.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-dynamically-change-quot-put-list-quot-in-Macro-depending/m-p/794110#M254599</link>
      <description>&lt;P&gt;It looks like you're creating a bunch of local macro vars. Since you just want to write values to the log&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%put _local_ ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;might be the easiest way to see it.&lt;/P&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;</description>
      <pubDate>Wed, 02 Feb 2022 21:20:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-dynamically-change-quot-put-list-quot-in-Macro-depending/m-p/794110#M254599</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2022-02-02T21:20:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to dynamically change "%put list " in Macro depending on the dataset.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-dynamically-change-quot-put-list-quot-in-Macro-depending/m-p/794111#M254600</link>
      <description>&lt;P&gt;Thanks for taking time. Please see my responses for your question in &lt;FONT color="#0000FF"&gt;blue.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Here's a slightly cleaned up version of your code,&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;but for your Y data set your drugs are numbered non-sequentially, ie drug7, no drug6 but 6 drugs. Do you want to be able to access those values dynamically?&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Ans: &lt;/STRONG&gt;&lt;FONT face="batang,apple gothic" size="4" color="#0000FF"&gt;Yes, the number can be any thing&amp;nbsp; for drug, I just given in sequentially for sample purpose in X and skipped in Y. So I believe , first approach will have the problem because of&amp;nbsp; _n_. thanks again for taking your time.&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Feb 2022 21:25:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-dynamically-change-quot-put-list-quot-in-Macro-depending/m-p/794111#M254600</guid>
      <dc:creator>SASuserlot</dc:creator>
      <dc:date>2022-02-02T21:25:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to dynamically change "%put list " in Macro depending on the dataset.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-dynamically-change-quot-put-list-quot-in-Macro-depending/m-p/794115#M254601</link>
      <description>&lt;P&gt;&amp;nbsp;Thanks. This my log, Is I am doing any thing wrong!&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SASuserlot_0-1643837604183.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/68166iCFDBFF36BFBD1FAE/image-size/medium?v=v2&amp;amp;px=400" role="button" title="SASuserlot_0-1643837604183.png" alt="SASuserlot_0-1643837604183.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Feb 2022 21:33:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-dynamically-change-quot-put-list-quot-in-Macro-depending/m-p/794115#M254601</guid>
      <dc:creator>SASuserlot</dc:creator>
      <dc:date>2022-02-02T21:33:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to dynamically change "%put list " in Macro depending on the dataset.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-dynamically-change-quot-put-list-quot-in-Macro-depending/m-p/794116#M254602</link>
      <description>&lt;P&gt;I'm guessing you might have created these macro variables as global macro variables earlier.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%put _user_ ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That will show all user-created macro variables (global and local). Generally, it's better to create local macro variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another option, if you want to print the value of all macro variables that start with DRUG, is to use the dictionary table/view that stores values of macro variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set sashelp.vmacro;
  where name like "DRUG%" ;  
  put name value ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 02 Feb 2022 21:39:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-dynamically-change-quot-put-list-quot-in-Macro-depending/m-p/794116#M254602</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2022-02-02T21:39:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to dynamically change "%put list " in Macro depending on the dataset.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-dynamically-change-quot-put-list-quot-in-Macro-depending/m-p/794118#M254603</link>
      <description>&lt;P&gt;Thank you for taking time. I use these macro variable some where down the road in tables creation for validation. So I would like to check them in the log instead of opening the dataset to compare my numbers. The code you mentioned is resolve&amp;nbsp; to drugs list, I can change that to count list. which&amp;nbsp; do my job.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Feb 2022 21:40:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-dynamically-change-quot-put-list-quot-in-Macro-depending/m-p/794118#M254603</guid>
      <dc:creator>SASuserlot</dc:creator>
      <dc:date>2022-02-02T21:40:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to dynamically change "%put list " in Macro depending on the dataset.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-dynamically-change-quot-put-list-quot-in-Macro-depending/m-p/794124#M254604</link>
      <description>Thank you.</description>
      <pubDate>Wed, 02 Feb 2022 22:00:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-dynamically-change-quot-put-list-quot-in-Macro-depending/m-p/794124#M254604</guid>
      <dc:creator>SASuserlot</dc:creator>
      <dc:date>2022-02-02T22:00:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to dynamically change "%put list " in Macro depending on the dataset.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-dynamically-change-quot-put-list-quot-in-Macro-depending/m-p/794127#M254605</link>
      <description>&lt;P&gt;So far,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt; solution and my other solution would allow you to deal with issue correctly and allow you to loop through. Otherwise if you're planning to use those variables later how will you know to skip the 6th index?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/350312"&gt;@SASuserlot&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks for taking time. Please see my responses for your question in &lt;FONT color="#0000FF"&gt;blue.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Here's a slightly cleaned up version of your code,&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;but for your Y data set your drugs are numbered non-sequentially, ie drug7, no drug6 but 6 drugs. Do you want to be able to access those values dynamically?&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Ans: &lt;/STRONG&gt;&lt;FONT face="batang,apple gothic" size="4" color="#0000FF"&gt;Yes, the number can be any thing&amp;nbsp; for drug, I just given in sequentially for sample purpose in X and skipped in Y. So I believe , first approach will have the problem because of&amp;nbsp; _n_. thanks again for taking your time.&lt;/FONT&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Feb 2022 22:15:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-dynamically-change-quot-put-list-quot-in-Macro-depending/m-p/794127#M254605</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-02-02T22:15:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to dynamically change "%put list " in Macro depending on the dataset.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-dynamically-change-quot-put-list-quot-in-Macro-depending/m-p/794132#M254608</link>
      <description>May be I did not  given the example good enough, sorry for that.  When you sending input dataset we usually have the idea what is drug names or drug numbers and any missing.  the 'count ' given in example  is the number came after proc freq. like out of 100 how many took drug 1 and how many drug 2.... etc. so I was indirectly looking for that.  we sort data by number so  that we know the order.</description>
      <pubDate>Wed, 02 Feb 2022 22:34:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-dynamically-change-quot-put-list-quot-in-Macro-depending/m-p/794132#M254608</guid>
      <dc:creator>SASuserlot</dc:creator>
      <dc:date>2022-02-02T22:34:12Z</dc:date>
    </item>
  </channel>
</rss>

