<?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: Creating macro variable from a table in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-macro-variable-from-a-table/m-p/464286#M285092</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro amex(d=,v=,l=,a=);
proc sql;
select count(distinct &amp;amp;v) into :n from &amp;amp;d;
select distinct &amp;amp;l into :mth separated by "," from &amp;amp;d;
select sum(spend) into :s1 from &amp;amp;d;
quit;
/* you just created three macro variables which are never used and will vanish once the macro ends, as they are local to the macro */

data &amp;amp;a;
set &amp;amp;d;
/* where is the run statement for the data step? Don't be sloppy */
%mend amex;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;All the macro does is making a copy of dataset &amp;amp;d in new dataset &amp;amp;a; if you add additional data step code AFTER the macro call, you might create some really nasty piece of code that is next to un-maintainable.&lt;/P&gt;</description>
    <pubDate>Wed, 23 May 2018 06:03:35 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2018-05-23T06:03:35Z</dc:date>
    <item>
      <title>Creating macro variable from a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-macro-variable-from-a-table/m-p/464276#M285090</link>
      <description>&lt;P&gt;data Customers;&lt;BR /&gt;input Cust_id Month Spend;&lt;BR /&gt;cards;&lt;BR /&gt;1 JAN 100&lt;BR /&gt;1 FEB 200&lt;BR /&gt;1 MAR 300&lt;BR /&gt;1 APR&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;BR /&gt;2 JAN 400&lt;BR /&gt;2 FEB 100&lt;BR /&gt;2 MAR 600&lt;BR /&gt;3 JUN 100&lt;BR /&gt;;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro amex(d=,v=,l=,a=);&lt;BR /&gt;proc sql;&lt;BR /&gt;select count(distinct &amp;amp;v) into :n from &amp;amp;d;&lt;BR /&gt;select distinct &amp;amp;l into :mth separated by "," from &amp;amp;d;&lt;BR /&gt;select sum(spend) into :s1 from &amp;amp;d;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;data &amp;amp;a;&lt;BR /&gt;set &amp;amp;d;&lt;/P&gt;&lt;P&gt;%mend amex;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%amex(d=customers,v=cust_id,l=Month,a=li);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;need help as im new to macros&lt;/P&gt;&lt;P&gt;I want :&amp;nbsp;&lt;/P&gt;&lt;P&gt;CUSTOMER_COUNT&amp;nbsp; AS COUNT OF DISTINCT CUSTOMER,&amp;nbsp; &amp;nbsp; &amp;nbsp; MONTH AS DISTINCT MONTH SEPARATED BY COMMA.&lt;/P&gt;&lt;P&gt;and TOTAL SPEND AS SUM OF SPEND&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;not getting desired output,plz tell what is wrong&lt;/P&gt;</description>
      <pubDate>Wed, 23 May 2018 04:46:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-macro-variable-from-a-table/m-p/464276#M285090</guid>
      <dc:creator>adi121</dc:creator>
      <dc:date>2018-05-23T04:46:44Z</dc:date>
    </item>
    <item>
      <title>Re: Creating macro variable from a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-macro-variable-from-a-table/m-p/464277#M285091</link>
      <description>&lt;P&gt;seems like just a syntax issue to me:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Customers;
input Cust_id Month $ Spend;
cards;
1 JAN 100
1 FEB 200
1 MAR 300
1 APR .
2 JAN 400
2 FEB 100
2 MAR 600
3 JUN 100
;run;

 

%macro amex(d=,v=,l=,a=);
proc sql;
select count(distinct &amp;amp;v) into :n from &amp;amp;d;
select distinct &amp;amp;l into :mth separated by "," from &amp;amp;d;
select sum(spend) into :s1 from &amp;amp;d;
quit;

%put &amp;amp;mth;
%put &amp;amp;n;
%put &amp;amp;s1;
data &amp;amp;a;
set &amp;amp;d;
run;
%mend amex;

 

%amex(d=customers,v=cust_id,l=Month,a=li);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 23 May 2018 05:00:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-macro-variable-from-a-table/m-p/464277#M285091</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-05-23T05:00:48Z</dc:date>
    </item>
    <item>
      <title>Re: Creating macro variable from a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-macro-variable-from-a-table/m-p/464286#M285092</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro amex(d=,v=,l=,a=);
proc sql;
select count(distinct &amp;amp;v) into :n from &amp;amp;d;
select distinct &amp;amp;l into :mth separated by "," from &amp;amp;d;
select sum(spend) into :s1 from &amp;amp;d;
quit;
/* you just created three macro variables which are never used and will vanish once the macro ends, as they are local to the macro */

data &amp;amp;a;
set &amp;amp;d;
/* where is the run statement for the data step? Don't be sloppy */
%mend amex;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;All the macro does is making a copy of dataset &amp;amp;d in new dataset &amp;amp;a; if you add additional data step code AFTER the macro call, you might create some really nasty piece of code that is next to un-maintainable.&lt;/P&gt;</description>
      <pubDate>Wed, 23 May 2018 06:03:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-macro-variable-from-a-table/m-p/464286#M285092</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-05-23T06:03:35Z</dc:date>
    </item>
    <item>
      <title>Re: Creating macro variable from a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-macro-variable-from-a-table/m-p/464292#M285093</link>
      <description>&lt;P&gt;"&lt;SPAN&gt;need help as im new to macros" - lesson one, only use macro where it actually adds any value - which is in very limited circumstances and should be clearly detailed in a functional design specification of the process.&amp;nbsp; Exact same code (but which will actually create the three macro variables as you want:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;data customers;
  input cust_id month spend;
cards;
1 JAN 100
1 FEB 200
1 MAR 300
1 APR 
2 JAN 400
2 FEB 100
2 MAR 600
3 JUN 100
;
run;

proc sql noprint;
  select count(distinct cust_id) into :n from customers;
  select distinct month into :mth separated by "," from customers;
  select sum(spend) into :s1 from customers;
quit;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;Next question, what do you plan on doing with the macro variables once created.&amp;nbsp; The second looks like some sort of list of data items, doing it this way will lead to errors, far less robust code, and more coding effort.&amp;nbsp; I can guarentee you there are simpler methods to do what you want to do.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 23 May 2018 06:58:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-macro-variable-from-a-table/m-p/464292#M285093</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-05-23T06:58:36Z</dc:date>
    </item>
    <item>
      <title>Re: Creating macro variable from a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-macro-variable-from-a-table/m-p/464549#M285094</link>
      <description>&lt;P&gt;%macro amex(v=);&lt;BR /&gt;proc sql;&lt;BR /&gt;select count(distinct &amp;amp;v),Month,sum(spend) as Total&lt;BR /&gt;into :n1-:n4,:m1-:m4,:t1-:t4 from Customers&lt;BR /&gt;quit;&lt;BR /&gt;%put &amp;amp;n1 &amp;amp;m1 &amp;amp;t1;&lt;BR /&gt;%put &amp;amp;n2 &amp;amp;m2 &amp;amp;t2;&lt;BR /&gt;%put &amp;amp;n3 &amp;amp;m3 &amp;amp;t3;&lt;BR /&gt;%put &amp;amp;n4 &amp;amp;m4 &amp;amp;t4;&lt;BR /&gt;%mend amex;&lt;/P&gt;&lt;P&gt;%amex(v=cust_id);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i have changed the code but still not correct result&lt;/P&gt;&lt;P&gt;output is&amp;nbsp;&lt;/P&gt;&lt;P&gt;3 JAN 1800&lt;/P&gt;&lt;P&gt;3 FEB&amp;nbsp;&lt;SPAN&gt;1800&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;3 MAR&amp;nbsp;&amp;nbsp;&lt;SPAN&gt;1800&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;3 APR&amp;nbsp;&lt;SPAN&gt;1800&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;but i want output like&amp;nbsp;&lt;/P&gt;&lt;P&gt;1 JAN,FEB,MAR,APR 600&lt;/P&gt;&lt;P&gt;2&amp;nbsp;&lt;SPAN&gt;JAN,FEB,MAR&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1100&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;3 jun&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;100&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Plz help&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 23 May 2018 19:40:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-macro-variable-from-a-table/m-p/464549#M285094</guid>
      <dc:creator>adi121</dc:creator>
      <dc:date>2018-05-23T19:40:32Z</dc:date>
    </item>
    <item>
      <title>Re: Creating macro variable from a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-macro-variable-from-a-table/m-p/464633#M285095</link>
      <description>&lt;P&gt;im near to my result&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro b;&lt;BR /&gt;proc sql;&lt;BR /&gt;select count(distinct cust_id),Month,sum(Spend) as Total&lt;BR /&gt;into :n1,:mth separated by ",",:t1 from Customers;&lt;BR /&gt;quit;&lt;BR /&gt;%put &amp;amp;n1 &amp;amp;mth &amp;amp;t1;&lt;BR /&gt;%mend b;&lt;BR /&gt;%b;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;output is : 3 JAN,FEB ,mar,APR,JAN,FEB,MAR,JUN&amp;nbsp; &amp;nbsp;1800&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;im not able to get distinct month&amp;nbsp;&lt;/P&gt;&lt;P&gt;plz help&lt;/P&gt;&lt;P&gt;when i use distinct Month error comes as:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Apparent symbolic reference N1 not resolved.&lt;BR /&gt;WARNING: Apparent symbolic reference MTH not resolved.&lt;BR /&gt;WARNING: Apparent symbolic reference T1 not resolved.&lt;BR /&gt;&amp;amp;n1 &amp;amp;mth &amp;amp;t1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;plz help.....&lt;/P&gt;</description>
      <pubDate>Thu, 24 May 2018 04:38:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-macro-variable-from-a-table/m-p/464633#M285095</guid>
      <dc:creator>adi121</dc:creator>
      <dc:date>2018-05-24T04:38:23Z</dc:date>
    </item>
    <item>
      <title>Re: Creating macro variable from a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-macro-variable-from-a-table/m-p/464639#M285096</link>
      <description>&lt;P&gt;It looks like you are having trouble with two things.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1) Scoping of macro variables.&amp;nbsp; The SELECT ... INTO syntax will write into existing macro variables.&amp;nbsp; But if you run it inside a macro and the target macro variables do not already exist they will be created as local to the macro. So they will disappear when the macro finishes executing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2) Defining what you want to select/count with your SQL.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;From your other self-reply you state that you want values like&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;but i want output like&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1 JAN,FEB,MAR,APR 600&lt;/P&gt;
&lt;P&gt;2&amp;nbsp;&lt;SPAN&gt;JAN,FEB,MAR&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1100&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;3 jun&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;100&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If you want that output then make a TABLE and skip the attempt to generate macro variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set customers;
  by cust_id month ;
  length months $200 ;
  if first.cust_id then TOTAL=0;
  total + spend ;
  if first.cust_id then months=' ';
  if first.month then months=catx(',',months,month);
  if last.cust_id;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;How are you planning to use the macro variables?&amp;nbsp; I suspect that for most applications the dataset would be of more value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 May 2018 05:35:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-macro-variable-from-a-table/m-p/464639#M285096</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-05-24T05:35:50Z</dc:date>
    </item>
  </channel>
</rss>

