<?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 macro variables using proc sql in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-macro-variables-using-proc-sql/m-p/326504#M72719</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Here is a related solution&lt;BR /&gt;&lt;BR /&gt;/* T008040 Placing counts in proc report column headers

inspired by
How to create macro variables using proc sql
https://goo.gl/EtsSFo
https://communities.sas.com/t5/Base-SAS-Programming/How-to-create-macro-variables-using-proc-sql/m-p/326471

Suppose you want to produce a report with a header
that contains 'treatment name and counts

HAVE

Up to 40 obs from SexGov total obs=50

Obs    SEX       GOV  IDS   SERIAL    PAY

  1    Female    FBI   13    25439   3463
  2    Female    FBI   14   629949  28614
  3    Female    FBI   17   851828  27102
  4    Female    FBI   20    66992  19882
  5    Female    FBI   23   659046  34665
  6    Female    FBI   25   248322  17126
....
 37    Male      CIA   17   786544  47492
 38    Male      CIA   21   716109  27873
 39    Male      CIA   22   731075  22004
 40    Male      CIA   23   933118  11092

WANT

               Female          Male
    GOV        N = 21        N = 29

    CIA             5             9
    FBI             6            10
    IRS            10            10


SOLUTION WORKING CODE
=====================

   resolve(catx(' ','%let',sex,'=%str(',sex,'# N =',put(count(*),2. -l),');'))


FULL SOLUTION
=============

   * create sample data;
   Data  SexGov;
   Do Sex='Female','Male';
     Do Gov='FBI','CIA','IRS';
       Do Ids=10 to 25;
           Serial=Int(1E6*Uniform(5739));
           Pay=Int(50000*Uniform(57343));
           If Uniform(5643) &amp;lt; .5 Then Output;
         End;
       End;
     End;
   Run;

   * create macro variable Male with  "Male # N=29";
   proc sql;
      create
         table hdr as
      select
         resolve(catx(' ','%let',sex,'=%str(',sex,'# N =',put(count(*),2. -l),');'))
        ,*
      from sexgov group by sex;quit;

   %put &amp;amp;=female;
   %put &amp;amp;=male;

   /*
   FEMALE= Female # N = 21
   MALE  = Male # N = 29
   */

   /* Put counts in Rectangular array */
   Ods Exclude All;
   Ods Output Observed=hdr(Rename=Label=Gov
       drop=sum where=(gov ne 'Sum'));
   Proc Corresp Data=SexGov Observed dim=1;
      Table Gov, Sex;
   Run;
   Ods Select All;

   * print report note the macro variables;
   proc report data=hdr nowd missing split='#' headskip;
     cols Gov Female Male;
     define Female /display "&amp;amp;female" width=12;
     define Male   /display "&amp;amp;male" width=12;
   run;quit;


              Female          Male
   GOV        N = 21        N = 29

   CIA             5             9
   FBI             6            10
   IRS            10            10



&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 21 Jan 2017 14:37:36 GMT</pubDate>
    <dc:creator>rogerjdeangelis</dc:creator>
    <dc:date>2017-01-21T14:37:36Z</dc:date>
    <item>
      <title>How to create macro variables using proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-macro-variables-using-proc-sql/m-p/326471#M72694</link>
      <description>&lt;P&gt;Dear,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to create macro variables for the output values produced using proc sql;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;my code:&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table one as&lt;BR /&gt;select count(distinct usubjid) as NS,TRT1&lt;BR /&gt;from data1&lt;BR /&gt;group by TRT1;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;output produced by sql code;&lt;/P&gt;&lt;P&gt;NS &amp;nbsp; &amp;nbsp; TRT1&lt;/P&gt;&lt;P&gt;50 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;A&lt;/P&gt;&lt;P&gt;60 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;B&lt;/P&gt;&lt;P&gt;40 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;C&lt;/P&gt;&lt;P&gt;150 &amp;nbsp; &amp;nbsp; &amp;nbsp; Total&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How to create 4 macro variables equal to A,B,C, and Total, so I can use these later in data step. Please help&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Sat, 21 Jan 2017 03:32:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-macro-variables-using-proc-sql/m-p/326471#M72694</guid>
      <dc:creator>knveraraju91</dc:creator>
      <dc:date>2017-01-21T03:32:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to create macro variables using proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-macro-variables-using-proc-sql/m-p/326472#M72695</link>
      <description>&lt;P&gt;Don't use SQL use a data step with CALL SYMPUTX. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;Proc SQL doesn't have a method for dynamically assigning macro variable names.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 21 Jan 2017 03:38:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-macro-variables-using-proc-sql/m-p/326472#M72695</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-01-21T03:38:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to create macro variables using proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-macro-variables-using-proc-sql/m-p/326478#M72701</link>
      <description>&lt;P&gt;I am sorry that if I didnot explain properly.&lt;/P&gt;&lt;P&gt;I am using PROC sql to get the values in my program. I also need to assign the values into a macro variables using INTO: so that I can use the values later in the step. Thank you&lt;/P&gt;</description>
      <pubDate>Sat, 21 Jan 2017 04:00:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-macro-variables-using-proc-sql/m-p/326478#M72701</guid>
      <dc:creator>knveraraju91</dc:creator>
      <dc:date>2017-01-21T04:00:19Z</dc:date>
    </item>
    <item>
      <title>Re: How to create macro variables using proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-macro-variables-using-proc-sql/m-p/326479#M72702</link>
      <description>&lt;P&gt;try the below step &lt;BR /&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;select count(distinct usubjid) into: TRT1-&lt;BR /&gt;from data1&lt;BR /&gt;group by TRT1 order by trt1;&lt;BR /&gt;quit;&lt;BR /&gt;&lt;BR /&gt;%put &amp;amp;TRT1 &amp;amp;TRT2 &amp;amp;TRT3 &amp;amp;TRT4;&lt;BR /&gt;&lt;BR /&gt;%let A=&amp;amp;TRT1;&lt;BR /&gt;%let B=&amp;amp;TRT2;&lt;BR /&gt;%let C=&amp;amp;TRT3;&lt;BR /&gt;%let TOTAL=&amp;amp;TRT4;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;This will create 4 macro variables like TRT1 ,TRT2, TRT3 and TRT4 with counts and macro variable names could be changed to as provided.&lt;/P&gt;</description>
      <pubDate>Sat, 21 Jan 2017 04:29:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-macro-variables-using-proc-sql/m-p/326479#M72702</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2017-01-21T04:29:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to create macro variables using proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-macro-variables-using-proc-sql/m-p/326480#M72703</link>
      <description>&lt;P&gt;I don't think you can create macro variables and a data set at the same time. Since you need another step use a data step.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I understood your question as you want to create 4 macro variables called A, B, C &amp;amp; Total which are one column and the values to be from the second column. This can't be done in a single SQL query either, or at least simply.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you just want all 4 numerical values into 4 different macro variables named somerging like var1-var4 or a single macro variable then use the examples in the documentation. However, given your experience on the forum I assumed you were asking a more complex question.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 21 Jan 2017 04:30:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-macro-variables-using-proc-sql/m-p/326480#M72703</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-01-21T04:30:21Z</dc:date>
    </item>
    <item>
      <title>Re: How to create macro variables using proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-macro-variables-using-proc-sql/m-p/326481#M72704</link>
      <description>I agree with &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt; to get the macro variable names with treatment names then the best is to use call symputx.&lt;BR /&gt;use the dataset you developed by proc sql which has the count and treatment variable like below &lt;BR /&gt;&lt;BR /&gt;NS     TRT1&lt;BR /&gt;50        A&lt;BR /&gt;60        B&lt;BR /&gt;40        C&lt;BR /&gt;150       Total&lt;BR /&gt;&lt;BR /&gt;then, try &lt;BR /&gt;&lt;BR /&gt;data one;&lt;BR /&gt;input NS     TRT1$13.;&lt;BR /&gt;cards;&lt;BR /&gt;50        A&lt;BR /&gt;60        B&lt;BR /&gt;40        C&lt;BR /&gt;150       Total&lt;BR /&gt;;&lt;BR /&gt;&lt;BR /&gt;data _null_;&lt;BR /&gt;set one;&lt;BR /&gt;call symputx(trt1,ns);&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;%put &amp;amp;a &amp;amp;b &amp;amp;c &amp;amp;total;&lt;BR /&gt;</description>
      <pubDate>Sat, 21 Jan 2017 04:40:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-macro-variables-using-proc-sql/m-p/326481#M72704</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2017-01-21T04:40:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to create macro variables using proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-macro-variables-using-proc-sql/m-p/326504#M72719</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Here is a related solution&lt;BR /&gt;&lt;BR /&gt;/* T008040 Placing counts in proc report column headers

inspired by
How to create macro variables using proc sql
https://goo.gl/EtsSFo
https://communities.sas.com/t5/Base-SAS-Programming/How-to-create-macro-variables-using-proc-sql/m-p/326471

Suppose you want to produce a report with a header
that contains 'treatment name and counts

HAVE

Up to 40 obs from SexGov total obs=50

Obs    SEX       GOV  IDS   SERIAL    PAY

  1    Female    FBI   13    25439   3463
  2    Female    FBI   14   629949  28614
  3    Female    FBI   17   851828  27102
  4    Female    FBI   20    66992  19882
  5    Female    FBI   23   659046  34665
  6    Female    FBI   25   248322  17126
....
 37    Male      CIA   17   786544  47492
 38    Male      CIA   21   716109  27873
 39    Male      CIA   22   731075  22004
 40    Male      CIA   23   933118  11092

WANT

               Female          Male
    GOV        N = 21        N = 29

    CIA             5             9
    FBI             6            10
    IRS            10            10


SOLUTION WORKING CODE
=====================

   resolve(catx(' ','%let',sex,'=%str(',sex,'# N =',put(count(*),2. -l),');'))


FULL SOLUTION
=============

   * create sample data;
   Data  SexGov;
   Do Sex='Female','Male';
     Do Gov='FBI','CIA','IRS';
       Do Ids=10 to 25;
           Serial=Int(1E6*Uniform(5739));
           Pay=Int(50000*Uniform(57343));
           If Uniform(5643) &amp;lt; .5 Then Output;
         End;
       End;
     End;
   Run;

   * create macro variable Male with  "Male # N=29";
   proc sql;
      create
         table hdr as
      select
         resolve(catx(' ','%let',sex,'=%str(',sex,'# N =',put(count(*),2. -l),');'))
        ,*
      from sexgov group by sex;quit;

   %put &amp;amp;=female;
   %put &amp;amp;=male;

   /*
   FEMALE= Female # N = 21
   MALE  = Male # N = 29
   */

   /* Put counts in Rectangular array */
   Ods Exclude All;
   Ods Output Observed=hdr(Rename=Label=Gov
       drop=sum where=(gov ne 'Sum'));
   Proc Corresp Data=SexGov Observed dim=1;
      Table Gov, Sex;
   Run;
   Ods Select All;

   * print report note the macro variables;
   proc report data=hdr nowd missing split='#' headskip;
     cols Gov Female Male;
     define Female /display "&amp;amp;female" width=12;
     define Male   /display "&amp;amp;male" width=12;
   run;quit;


              Female          Male
   GOV        N = 21        N = 29

   CIA             5             9
   FBI             6            10
   IRS            10            10



&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 21 Jan 2017 14:37:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-macro-variables-using-proc-sql/m-p/326504#M72719</guid>
      <dc:creator>rogerjdeangelis</dc:creator>
      <dc:date>2017-01-21T14:37:36Z</dc:date>
    </item>
  </channel>
</rss>

