<?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 Can I Generate Macro Variables in proc SQL appropriately? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-Can-I-Generate-Macro-Variables-in-proc-SQL-appropriately/m-p/610423#M177761</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12887"&gt;@ErikLund_Jensen&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I do not think there is that much of a problem with the "upper limit", you can just as well use a larger number. Only the macro variables actually written to are created anyway.&lt;/P&gt;
&lt;P&gt;I think the SQL solution is much simpler and more elegant. It can easily be modified to use the exact number of values, if that is required (the disadvantage is slightly slower performance):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  select count(distinct paramn) into :param_cnt trimmed from test;
  select count(distinct subjid) into :_nobs_1-:_nobs_&amp;amp;param_cnt
  from test 
  group by paramn
  order by paramn;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 09 Dec 2019 09:44:54 GMT</pubDate>
    <dc:creator>s_lassen</dc:creator>
    <dc:date>2019-12-09T09:44:54Z</dc:date>
    <item>
      <title>How Can I Generate Macro Variables in proc SQL appropriately?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-Can-I-Generate-Macro-Variables-in-proc-SQL-appropriately/m-p/609853#M177563</link>
      <description>&lt;P&gt;Hello, there:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to generate macro variable, PARAMN1-PARAMN5, and I am trying to convert repeated structure of my code into macro.&lt;/P&gt;&lt;P&gt;But can't get macro variables I want. Please let me know if you know why this problem happens and how to resolve.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you in advance.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
    do paramn=1 to 5 by 1;
        do subjid=1 to 5 by 1;
            output;
        end;
    end;
run;


* Separated SELECT Clause &amp;lt;-- n.p. ;
proc sql noprint;
    select count(distinct subjid) into :_nobs_1
    from work.test(where=(paramn eq 1))
    ;
    select count(distinct subjid) into :_nobs_2
    from work.test(where=(paramn eq 2))
    ;
quit;
%put &amp;amp;_nobs_1.;
%put &amp;amp;_nobs_2.;

 
* in macro &amp;lt;-- Problem!! ;
proc sql noprint;
%macro gen_nobs(paramn=);
    select count(distinct subjid) into :_nobs_&amp;amp;paramn.
    from work.test(where=(paramn eq &amp;amp;paramn.))
    ;
%mend gen_nobs;
%gen_nobs(paramn=1)
%gen_nobs(paramn=2)
%gen_nobs(paramn=3)
%gen_nobs(paramn=4)
%gen_nobs(paramn=5)
quit;
%put &amp;amp;_nobs_1.;
%put &amp;amp;_nobs_2.;
%put &amp;amp;_nobs_3.;
%put &amp;amp;_nobs_4.;
%put &amp;amp;_nobs_5.;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 06 Dec 2019 03:51:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-Can-I-Generate-Macro-Variables-in-proc-SQL-appropriately/m-p/609853#M177563</guid>
      <dc:creator>KentaMURANAKA</dc:creator>
      <dc:date>2019-12-06T03:51:16Z</dc:date>
    </item>
    <item>
      <title>Re: How Can I Generate Macro Variables in proc SQL appropriately?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-Can-I-Generate-Macro-Variables-in-proc-SQL-appropriately/m-p/609861#M177564</link>
      <description>&lt;P&gt;Please try the below code&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro gen_nobs(paramn=);
%global _nobs_&amp;amp;paramn.;
proc sql noprint;
    select count(distinct subjid) into :_nobs_&amp;amp;paramn.
    from work.test(where=(paramn eq &amp;amp;paramn.))
    ;quit;
%mend gen_nobs;
%gen_nobs(paramn=1)
%gen_nobs(paramn=2)
%gen_nobs(paramn=3)
%gen_nobs(paramn=4)
%gen_nobs(paramn=5)

%put &amp;amp;_nobs_1.;
%put &amp;amp;_nobs_2.;
%put &amp;amp;_nobs_3.;
%put &amp;amp;_nobs_4.;
%put &amp;amp;_nobs_5.;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 06 Dec 2019 05:28:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-Can-I-Generate-Macro-Variables-in-proc-SQL-appropriately/m-p/609861#M177564</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2019-12-06T05:28:33Z</dc:date>
    </item>
    <item>
      <title>Re: How Can I Generate Macro Variables in proc SQL appropriately?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-Can-I-Generate-Macro-Variables-in-proc-SQL-appropriately/m-p/609893#M177570</link>
      <description>&lt;P&gt;The problem may be that the macro variables created in SQL are by default local to the macro. So they will not exist once you exit the macro. This can be solved by declaring the macro variables %GLOBAL in the macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another possibility is to create all the variables in one fell swoop, like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  select count(distinct subjid) into :_nobs_1-:_nobs_999
  from test 
  group by paramn
  order by paramn;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you do not know the exact number of PARAMN variables, or their values, it may be a good idea to get their values into macro variables as well:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  select paramn,count(distinct subjid) into :param1-:param999,:_nobs_1-:_nobs_999
  from test 
  group by paramn
  order by paramn;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can then use the automatic macro variable SQLOBS to find the number of distinct PARAMN values:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _NULL_;
  do _N_=1 to &amp;amp;sqlobs;
    p=symget(cats('param',_N_));
    n=symget(cats('_nobs_',_N_));
    put p= n=;
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Dec 2019 08:46:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-Can-I-Generate-Macro-Variables-in-proc-SQL-appropriately/m-p/609893#M177570</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2019-12-06T08:46:18Z</dc:date>
    </item>
    <item>
      <title>Re: How Can I Generate Macro Variables in proc SQL appropriately?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-Can-I-Generate-Macro-Variables-in-proc-SQL-appropriately/m-p/609987#M177619</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76464"&gt;@s_lassen&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;May I suggest a small change: It the counting is done in the proc sql step, but the creation of macro variables is done in a data step, there is no need for an upper limit to the number of macro variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;* Make some test data with different numbers of subjects
  and a dublet record to test for distinct counting; 
data test;
    do paramn=1 to 5 by 1;
        do subjid=1 to paramn+3 by 1;
            output;
        end;
    end;
	paramn = 5; subjid = 3; output;
run;

* Distinct count of parameters and subjects;
proc sql noprint;
	create table a as 
    	select distinct paramn, count(distinct subjid) as nobs
	 	from work.test
		group by paramn;
quit;

* Place in macro variables;
data _null_; set a end=eof;
	n = left(put(_N_,8.));
	call symputx('_nobs_'||strip(n), nobs);
	call symputx('param_'||strip(n), paramn);
	put paramn= nobs=;
	if eof then call symputx('param_cnt',n);
run;
%put &amp;amp;=param_cnt;

* Test existence and value of macro variables;
%macro test;
	%do i = 1 %to &amp;amp;param_cnt;
		%put parameter: &amp;amp;&amp;amp;param_&amp;amp;i - subjects: &amp;amp;&amp;amp;_nobs_&amp;amp;i;
	%end;
%mend;
%test;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 06 Dec 2019 15:32:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-Can-I-Generate-Macro-Variables-in-proc-SQL-appropriately/m-p/609987#M177619</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2019-12-06T15:32:06Z</dc:date>
    </item>
    <item>
      <title>Re: How Can I Generate Macro Variables in proc SQL appropriately?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-Can-I-Generate-Macro-Variables-in-proc-SQL-appropriately/m-p/610423#M177761</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12887"&gt;@ErikLund_Jensen&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I do not think there is that much of a problem with the "upper limit", you can just as well use a larger number. Only the macro variables actually written to are created anyway.&lt;/P&gt;
&lt;P&gt;I think the SQL solution is much simpler and more elegant. It can easily be modified to use the exact number of values, if that is required (the disadvantage is slightly slower performance):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  select count(distinct paramn) into :param_cnt trimmed from test;
  select count(distinct subjid) into :_nobs_1-:_nobs_&amp;amp;param_cnt
  from test 
  group by paramn
  order by paramn;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 09 Dec 2019 09:44:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-Can-I-Generate-Macro-Variables-in-proc-SQL-appropriately/m-p/610423#M177761</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2019-12-09T09:44:54Z</dc:date>
    </item>
  </channel>
</rss>

