<?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 create a list of macro variables using proc SQL? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-list-of-macro-variables-using-proc-SQL/m-p/341462#M78162</link>
    <description>Can you elaborate more on the group processing? Is it another feature of SAS?</description>
    <pubDate>Thu, 16 Mar 2017 08:16:28 GMT</pubDate>
    <dc:creator>afiqcjohari</dc:creator>
    <dc:date>2017-03-16T08:16:28Z</dc:date>
    <item>
      <title>How to create a list of macro variables using proc SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-list-of-macro-variables-using-proc-SQL/m-p/341449#M78153</link>
      <description>&lt;P&gt;First I want to get the distinct list of the variable.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here let's say I have 4 different levels for var.&lt;/P&gt;&lt;P&gt;But what if I have unknown different levels? Is there a way to generate the macro variables on the fly?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;proc sql;
&amp;nbsp; &amp;nbsp;select distinct var into :v1 -:v4 from t;
quit;&lt;/PRE&gt;&lt;P&gt;Then for each of them, I want to loop over a series of macros.&lt;/P&gt;&lt;P&gt;For example&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;%macroone(a = &amp;amp;v1.);
%macrotwo(a = &amp;amp;v1.);&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;Is there such a thing as a macro array where I can loop using the position of the array?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Such as &lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;%macroone(a= &amp;amp;v[1].);&lt;/PRE&gt;</description>
      <pubDate>Thu, 16 Mar 2017 07:47:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-list-of-macro-variables-using-proc-SQL/m-p/341449#M78153</guid>
      <dc:creator>afiqcjohari</dc:creator>
      <dc:date>2017-03-16T07:47:15Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a list of macro variables using proc SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-list-of-macro-variables-using-proc-SQL/m-p/341452#M78155</link>
      <description>&lt;P&gt;Call execute. See the second example in the documentation.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Mar 2017 07:53:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-list-of-macro-variables-using-proc-SQL/m-p/341452#M78155</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-03-16T07:53:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a list of macro variables using proc SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-list-of-macro-variables-using-proc-SQL/m-p/341454#M78157</link>
      <description>&lt;P&gt;Instead of storing your distinct values (how can you guarantee there are only four?) in macro variables, store them in a new dataset, and then do a data _null_ step from that, and use call execute to call your macro for every distinct item. That scales indefinitely.&lt;/P&gt;</description>
      <pubDate>Thu, 16 Mar 2017 08:00:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-list-of-macro-variables-using-proc-SQL/m-p/341454#M78157</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-03-16T08:00:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a list of macro variables using proc SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-list-of-macro-variables-using-proc-SQL/m-p/341455#M78158</link>
      <description>Yes, so I thought of calculating the number of distinct values first. Use this number to generate as many macro variables needed.&lt;BR /&gt;&lt;BR /&gt;I'd need the macro variables as a suffix to one of the macro outputs. Not sure whether data_null_step allows the latter.</description>
      <pubDate>Thu, 16 Mar 2017 08:03:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-list-of-macro-variables-using-proc-SQL/m-p/341455#M78158</guid>
      <dc:creator>afiqcjohari</dc:creator>
      <dc:date>2017-03-16T08:03:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a list of macro variables using proc SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-list-of-macro-variables-using-proc-SQL/m-p/341458#M78160</link>
      <description>&lt;P&gt;You can put the complete list into a single macro variable (as long as none of the values contain embedded blanks). &amp;nbsp;Then process each item in the list. &amp;nbsp;Here are the basics:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.sas.com/content/sastraining/2015/01/30/sas-authors-tip-getting-the-macro-language-to-perform-a-do-loop-over-a-list-of-values/" target="_blank"&gt;http://blogs.sas.com/content/sastraining/2015/01/30/sas-authors-tip-getting-the-macro-language-to-perform-a-do-loop-over-a-list-of-values/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Mar 2017 08:08:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-list-of-macro-variables-using-proc-SQL/m-p/341458#M78160</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-03-16T08:08:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a list of macro variables using proc SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-list-of-macro-variables-using-proc-SQL/m-p/341460#M78161</link>
      <description>&lt;P&gt;Make your macro self-contained, so that it works as a "black box" (no side effects, all parameters appear in the macro definition). Names of output datasets should be derived from the parameters or supplied as a separate parameter.&lt;/P&gt;
&lt;P&gt;Then you store all needed parameter values in the dataset and do&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
set control_dataset;
call execute('%macro_call(param1='!!strip(param1)!!',param2='!!strip(param2)!!');');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Be aware that macro logic will be executed as soon as you place the macro in the queue with call execute (while your data _null_ is still running), but all data and proc steps created from that will only start after the data _null_ has finished. So you should not create new macro variables in data steps for further use within the macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And finally, often operations like you seem to have in mind can be handled with by group processing, without the need for macro processing at all.&lt;/P&gt;</description>
      <pubDate>Thu, 16 Mar 2017 08:13:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-list-of-macro-variables-using-proc-SQL/m-p/341460#M78161</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-03-16T08:13:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a list of macro variables using proc SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-list-of-macro-variables-using-proc-SQL/m-p/341462#M78162</link>
      <description>Can you elaborate more on the group processing? Is it another feature of SAS?</description>
      <pubDate>Thu, 16 Mar 2017 08:16:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-list-of-macro-variables-using-proc-SQL/m-p/341462#M78162</guid>
      <dc:creator>afiqcjohari</dc:creator>
      <dc:date>2017-03-16T08:16:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a list of macro variables using proc SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-list-of-macro-variables-using-proc-SQL/m-p/341471#M78168</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/130031"&gt;@afiqcjohari&lt;/a&gt; wrote:&lt;BR /&gt;Can you elaborate more on the group processing? Is it another feature of SAS?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;By-group processing is one of THE features of SAS.&lt;/P&gt;
&lt;P&gt;Compare this&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro means(group);

data class;
set sashelp.class;
where sex = "&amp;amp;group";
run;

proc means data=class;
var age;
output out=class_&amp;amp;group;
run;

%mend;

proc sql noprint;
select count(distinct(sex)) into :varcount from sashelp.class;
quit;

%let varcount=%sysfunc(strip(&amp;amp;varcount));

proc sql noprint;
select distinct(sex) into :var1 - :var&amp;amp;varcount from sashelp.class;
quit;

%macro run_all;
%do i = 1 %to &amp;amp;varcount;
%means(&amp;amp;&amp;amp;var&amp;amp;i);
%end;
%mend;

%run_all;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;With this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=sashelp.class out=class;
by sex;
run;

proc means data=class;
by sex;
var age;
output out=class_means;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 16 Mar 2017 08:37:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-list-of-macro-variables-using-proc-SQL/m-p/341471#M78168</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-03-16T08:37:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a list of macro variables using proc SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-list-of-macro-variables-using-proc-SQL/m-p/341619#M78217</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Without knowing the number of levels of a variable create means output for each level

inspired by
https://goo.gl/8Bf1ap
https://communities.sas.com/t5/Base-SAS-Programming/How-to-create-a-list-of-macro-variables-using-proc-SQL/m-p/341449

HAVE
====

Up to 40 obs from sashelp.class total obs=19

Obs    NAME       SEX    AGE    HEIGHT    WEIGHT

  1    Alfred      M      14     69.0      112.5
  2    Alice       F      13     56.5       84.0
  3    Barbara     F      13     65.3       98.0
  4    Carol       F      14     62.8      102.5
  5    Henry       M      14     63.5      102.5
  6    James       M      12     57.3       83.0
  7    Jane        F      12     59.8       84.5
  8    Janet       F      15     62.5      112.5
  9    Jeffrey     M      13     62.5       84.0
 10    John        M      12     59.0       99.5
 11    Joyce       F      11     51.3       50.5
 12    Judy        F      14     64.3       90.0
 13    Louise      F      12     56.3       77.0
 14    Mary        F      15     66.5      112.0
 15    Philip      M      16     72.0      150.0
 16    Robert      M      12     64.8      128.0
 17    Ronald      M      15     67.0      133.0
 18    Thomas      M      11     57.5       85.0
 19    William     M      15     66.5      112.0

WANT ( proc means listing and  output dataset by levels of variable)
=====================================================================

Up to 40 obs from CLASS_M total obs=5

Obs    _TYPE_    _FREQ_    _STAT_      AGE

 1        0        10       N        10.0000
 2        0        10       MIN      11.0000
 3        0        10       MAX      16.0000
 4        0        10       MEAN     13.4000
 5        0        10       STD       1.6465


Up to 40 obs from CLASS_F total obs=5

Obs    _TYPE_    _FREQ_    _STAT_      AGE

 1        0         9       N         9.0000
 2        0         9       MIN      11.0000
 3        0         9       MAX      15.0000
 4        0         9       MEAN     13.2222
 5        0         9       STD       1.3944

Sex=F
                     Analysis Variable : AGE

 N            Mean         Std Dev         Minimum         Maximum
------------------------------------------------------------------
 9      13.2222222       1.3944334      11.0000000      15.0000000
------------------------------------------------------------------

Sex=F
                     Analysis Variable : AGE

 N            Mean         Std Dev         Minimum         Maximum
------------------------------------------------------------------
10      13.4000000       1.6465452      11.0000000      16.0000000
------------------------------------------------------------------


WORKING CODE
============


      select quote(max(sex)) into :sex separated by ","
         do grp=&amp;amp;sex;
           call symputx('grp',grp);
           dosubl
              proc means data=sashelp.class(where=(sex="&amp;amp;grp"));
              output out=class_&amp;amp;grp.

FULL SOLUTION
=============

proc datasets lib=work kill;
run;quit;

%symdel grp sex;
data _null_;

  if _n_=0 then do;
    %let rc=%sysfunc(dosubl('
      proc sql noprint;
       select quote(max(sex)) into :sex separated by ","
       from sashelp.class group by sex
      ;quit;
    '));
  end;

   do grp=&amp;amp;sex;
      call symputx('grp',grp);
      rc=dosubl('
         proc means data=sashelp.class(where=(sex="&amp;amp;grp"));
           title "Sex=&amp;amp;grp";
           var age;
           output out=class_&amp;amp;grp.;
         run;quit;
      ');
   end;

   stop;
run;quit;


SYMBOLGEN:  Macro variable GRP resolves to F
NOTE: There were 9 observations read from the data set SASHELP.CLASS.
      WHERE sex='F';
NOTE: The data set WORK.CLASS_F has 5 observations and 4 variables.
NOTE: PROCEDURE MEANS used (Total process time):
      real time           0.09 seconds

SYMBOLGEN:  Macro variable GRP resolves to M
NOTE: There were 10 observations read from the data set SASHELP.CLASS.
      WHERE sex='M';
NOTE: The data set WORK.CLASS_M has 5 observations and 4 variables.
NOTE: PROCEDURE MEANS used (Total process time):
      real time           0.06 seconds
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 16 Mar 2017 15:18:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-list-of-macro-variables-using-proc-SQL/m-p/341619#M78217</guid>
      <dc:creator>rogerjdeangelis</dc:creator>
      <dc:date>2017-03-16T15:18:42Z</dc:date>
    </item>
  </channel>
</rss>

