<?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/465354#M285133</link>
    <description>&lt;P&gt;hi tom,&lt;/P&gt;&lt;P&gt;you are a genius.&lt;/P&gt;&lt;P&gt;the code gives perfect result.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;but tom,i had 1 doubt.how&amp;nbsp;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;monthnum &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;month&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token keyword"&gt;input&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;cats&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;month&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1960&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;date9&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;how this statement is working .im not able to understand the functioning of this statement.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 27 May 2018 19:40:27 GMT</pubDate>
    <dc:creator>adi121</dc:creator>
    <dc:date>2018-05-27T19:40:27Z</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/464872#M285127</link>
      <description>&lt;P&gt;&lt;BR /&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;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 c;&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 c;&lt;BR /&gt;%c;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;im getting somewhat correct output but not able to get distinct months&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i want output as&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;3 JAN,FEB,MAR,APR,JUN 1800&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;but im getting output as&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;3 JAN,FEB,MAR,APR,JAN,FEB,MAR,JUN 1800&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;how to get distinct months ???????????&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;and also how to store the result in dataset??&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 May 2018 19:01:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-macro-variable-from-a-table/m-p/464872#M285127</guid>
      <dc:creator>adi121</dc:creator>
      <dc:date>2018-05-24T19:01:00Z</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/464878#M285128</link>
      <description>&lt;P&gt;You wrote-&lt;EM&gt;"&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;how to get distinct months ???????????&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;and also how to store the result in dataset??"&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am afraid&amp;nbsp; a single select query will most likely not do that because summary stats will remerge unless you have grouping categorical variables, however you could accomplish using a datastep&lt;/P&gt;&lt;P&gt;In my opinion proc sql is not a great idea for this task&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To go even further, If you are working in a production environment, having macro vars with concatenated values from an entire dataset doesn't help the performance. You are better off having a look up dataset.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 May 2018 19:19:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-macro-variable-from-a-table/m-p/464878#M285128</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-05-24T19:19:17Z</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/464899#M285129</link>
      <description>&lt;P&gt;Your data step is incorrect. You have to read the month as character:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;input Cust_id Month $ Spend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the other variables are working as needed then a separate query for the months.&lt;/P&gt;
&lt;P&gt;HOWEVER since your "month" variable is character valued do you want the result in a specific order? or does&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql noprint;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; select distinct month into :mth separated by ','&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; from customers;&lt;/P&gt;
&lt;P&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;work well enough?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you need this to sort in calendar order you need to say so. I would expect to see Apr first.&lt;/P&gt;</description>
      <pubDate>Thu, 24 May 2018 20:29:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-macro-variable-from-a-table/m-p/464899#M285129</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-05-24T20:29:13Z</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/464935#M285130</link>
      <description>&lt;P&gt;%macro c;&lt;BR /&gt;proc sql;&lt;BR /&gt;select count(distinct cust_id),sum(Spend) as Total&lt;BR /&gt;into :n1,:t1 from Customers ;&lt;BR /&gt;quit;&lt;BR /&gt;proc sql noprint;&lt;BR /&gt;select distinct month into :mth separated by ',' from customers;&lt;BR /&gt;quit;&lt;BR /&gt;%put &amp;amp;n1 &amp;amp;mth &amp;amp;t1;&lt;BR /&gt;%mend c;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;for getting months i have used a separate statement for months&lt;/P&gt;&lt;P&gt;but output is coming as&amp;nbsp;&lt;/P&gt;&lt;P&gt;3 APR,FEB,JAN,JUN,MAR 1800&lt;/P&gt;&lt;P&gt;how can i sort the months so that it appears as jan to apr&lt;/P&gt;&lt;P&gt;and 1 more doubt why we are using &lt;SPAN&gt;separate statement for months ,why it is not working in the same statement??&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 25 May 2018 03:20:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-macro-variable-from-a-table/m-p/464935#M285130</guid>
      <dc:creator>adi121</dc:creator>
      <dc:date>2018-05-25T03:20:41Z</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/464942#M285131</link>
      <description>&lt;P&gt;If you want only one copy of each value of MONTH then you need to add the DISTINCT keyword after SELECT.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;SAS will happily re-merge the summary statistics for grand total and unique ID counts back onto the detailed observations produced by multiple values of MONTH. So every observation will have the same values for those two columns.&amp;nbsp; But if you don't use the SEPARATED BY clause in the INTO clause then SAS will only place one copy into the macro variable.&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;
monthnum =month(input(cats(1,month,1960),date9.));
cards;
1 JAN 100
1 FEB 200
1 MAR 300
1 APR .
2 JAN 400
2 FEB 100
2 MAR 600
3 JUN 100
;

proc sql noprint;
  select distinct
         month
       , count(distinct cust_id) format=best32.
       , sum(spend) format=best32.
       , monthnum
    into :month_list separated by ' '
       , :customers trimmed
       , :total trimmed
       , :dummy
  from customers 
  order by monthnum
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;122   %put &amp;amp;=month_list;
MONTH_LIST=JAN FEB MAR APR JUN
123   %put &amp;amp;=customers;
CUSTOMERS=3
124   %put &amp;amp;=total;
TOTAL=1800
&lt;/PRE&gt;
&lt;P&gt;I changed it to use space as the delimiter between the multiple MONTH values as commas can be a major pain to use with SAS.&amp;nbsp; Most SAS syntax uses spaces as delimiters and not commas.&lt;/P&gt;</description>
      <pubDate>Fri, 25 May 2018 05:35:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-macro-variable-from-a-table/m-p/464942#M285131</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-05-25T05:35:42Z</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/464985#M285132</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/211005"&gt;@adi121&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;And now after getting answers to your question: What are you actually trying to achieve here in the end? WHY are you doing this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Reason I'm asking: Looks not like a very common coding approach to me which raises the question if there could be a simpler more common approach to solve your actual problem. What's the bigger picture?&lt;/P&gt;</description>
      <pubDate>Fri, 25 May 2018 08:19:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-macro-variable-from-a-table/m-p/464985#M285132</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2018-05-25T08:19:26Z</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/465354#M285133</link>
      <description>&lt;P&gt;hi tom,&lt;/P&gt;&lt;P&gt;you are a genius.&lt;/P&gt;&lt;P&gt;the code gives perfect result.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;but tom,i had 1 doubt.how&amp;nbsp;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;monthnum &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;month&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token keyword"&gt;input&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;cats&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;month&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1960&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;date9&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;how this statement is working .im not able to understand the functioning of this statement.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 27 May 2018 19:40:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-macro-variable-from-a-table/m-p/465354#M285133</guid>
      <dc:creator>adi121</dc:creator>
      <dc:date>2018-05-27T19:40:27Z</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/465355#M285134</link>
      <description>hi patrick,&lt;BR /&gt;what you are saying is right.&lt;BR /&gt;but this question was asked to me at an interview.&lt;BR /&gt;So i gave the same reply what you gave .&lt;BR /&gt;I could easily do this using data set approach but the interview panel was interested for more complex way.i wonder why they ask complex methods when it can be done in a very simple way..</description>
      <pubDate>Sun, 27 May 2018 19:45:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-macro-variable-from-a-table/m-p/465355#M285134</guid>
      <dc:creator>adi121</dc:creator>
      <dc:date>2018-05-27T19:45:09Z</dc:date>
    </item>
  </channel>
</rss>

