<?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 sum variable based on other variables for 1000 dataset using MACRO in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-sum-variable-based-on-other-variables-for-1000-dataset/m-p/441399#M110422</link>
    <description>&lt;P&gt;Thank you!&lt;/P&gt;</description>
    <pubDate>Thu, 01 Mar 2018 22:04:42 GMT</pubDate>
    <dc:creator>yanshuai</dc:creator>
    <dc:date>2018-03-01T22:04:42Z</dc:date>
    <item>
      <title>How to sum variable based on other variables for 1000 dataset using MACRO</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-sum-variable-based-on-other-variables-for-1000-dataset/m-p/441073#M110284</link>
      <description>&lt;DIV class="lia-message-body"&gt;&lt;DIV class="lia-message-body-content"&gt;&lt;P&gt;Hello all,&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;I have a thousand dataset, and each of them like this:&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt; have &lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token keyword"&gt;input&lt;/SPAN&gt;
&lt;SPAN class="token function"&gt;YEAR&lt;/SPAN&gt; SALE &lt;SPAN class="token keyword"&gt;ID&lt;/SPAN&gt; &lt;SPAN class="token punctuation"&gt;$&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;5&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 datalines"&gt;&lt;SPAN class="token keyword"&gt;datalines&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;SPAN class="token data string"&gt;2001  12  ab34
2001  10  1234
2001  14  ab34
2001  10  2234
2002  10  1234
2002  10  ab34
2002  10  2234
2002  10  2234
2001  12  ab34
2001  10  1234
2001  14  2234
2001  10  2234
2002  10  ab34
2002  10  1234
2002  10  ab34
2002  10  2234&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;run&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;For each dataset, I would like to sum all the SALE winthin same YEAR whose ID start with a letter, and sum the SALE within same YEAR only. I know how to run this with the following code. Thanks to those guys in my another post&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;proc&lt;/SPAN&gt; &lt;SPAN class="token procnames"&gt;sql&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
create &lt;SPAN class="token statement"&gt;table&lt;/SPAN&gt; want as
&lt;SPAN class="token statement"&gt;select&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;YEAR&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;
    &lt;SPAN class="token function"&gt;sum&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;SALE&lt;SPAN class="token operator"&gt;*&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;anyalpha&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;first&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token keyword"&gt;ID&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; as SALE1&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;
    &lt;SPAN class="token function"&gt;sum&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;SALE&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; as TOTALSALE
&lt;SPAN class="token keyword"&gt;from&lt;/SPAN&gt; have
&lt;SPAN class="token keyword"&gt;group&lt;/SPAN&gt; &lt;SPAN class="token statement"&gt;by&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;YEAR&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;quit&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;_But how can I do this for each of 1000 dataset. It is impossible to do this manually. Really urgent. How can I repeat this using MACRO.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;---------------------------------------------------------------------------------------------------updated 02-28-2018&lt;/P&gt;&lt;P&gt;I figure it out guys!!!!!!!!!!!!! This is the code I use:&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token macrobound"&gt;%macro&lt;/SPAN&gt; doit&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;memname&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;proc&lt;/SPAN&gt; &lt;SPAN class="token procnames"&gt;sql&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
create &lt;SPAN class="token statement"&gt;table&lt;/SPAN&gt; want&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;memname as
&lt;SPAN class="token statement"&gt;select&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;YEAR&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;
    &lt;SPAN class="token function"&gt;sum&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;SALE&lt;SPAN class="token operator"&gt;*&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;anyalpha&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;first&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token keyword"&gt;ID&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; as SALE1&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;
    &lt;SPAN class="token function"&gt;sum&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;SALE&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; as TOTALSALE&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;
&lt;SPAN class="token keyword"&gt;from&lt;/SPAN&gt; have&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;memname
&lt;SPAN class="token keyword"&gt;group&lt;/SPAN&gt; &lt;SPAN class="token statement"&gt;by&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;YEAR&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;quit&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token macrobound"&gt;%mend&lt;/SPAN&gt; doit&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;

&lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt; _null_&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token keyword"&gt;set&lt;/SPAN&gt; work&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;member&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;      &lt;SPAN class="token comment"&gt;/* this is a list I created before hand, it contains all the member name of the datasets I have*/&lt;/SPAN&gt;
call &lt;SPAN class="token keyword"&gt;execute&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'%doit('&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;!!&lt;/SPAN&gt;memname&lt;SPAN class="token operator"&gt;!!&lt;/SPAN&gt;&lt;SPAN class="token string"&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 procnames"&gt;run&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Now I have the variables I want, but they all in individual dataset.&lt;/P&gt;&lt;P&gt;So the question now is how can I add another variable for each dataset accoeding to its file name?&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Thu, 01 Mar 2018 18:58:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-sum-variable-based-on-other-variables-for-1000-dataset/m-p/441073#M110284</guid>
      <dc:creator>yanshuai</dc:creator>
      <dc:date>2018-03-01T18:58:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to sum variable based on other variables for 1000 dataset using MACRO</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-sum-variable-based-on-other-variables-for-1000-dataset/m-p/441109#M110301</link>
      <description>&lt;P&gt;One thousand data sets?&amp;nbsp; Do they have a common name structure?&amp;nbsp; If they do (say they all are start with ABC, as in ABC001 ABC002 ABCxyz ABC999), and if they all have the same variables, then you could&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data need/ view=need;
   set abc:   open=defer;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then run your proc sql using NEED instead of HAVE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It will read all the dataset files ABCxxx ABCyyy ABC... etc, into a data set VIEW named NEED.&amp;nbsp;&amp;nbsp; But that won't happen until NEED is used in a subsequent procedure.&amp;nbsp; As a result NEED will not be written to disk, but instead fed directly into PRCO SQL, saving lots of disk activity and space.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If they don't have a simple name structure, you could still make a view called NEED, but the SET statement will have more arguments ... lots more.&lt;/P&gt;</description>
      <pubDate>Thu, 01 Mar 2018 03:53:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-sum-variable-based-on-other-variables-for-1000-dataset/m-p/441109#M110301</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-03-01T03:53:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to sum variable based on other variables for 1000 dataset using MACRO</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-sum-variable-based-on-other-variables-for-1000-dataset/m-p/441112#M110302</link>
      <description>&lt;P&gt;You could do it all at once using a dataset list,&amp;nbsp;without macro processing. Suppose your datasets are called SET1, SET2, ... SET1000 in Library myLib, you could do:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data allMyData;
length dsn $41;
set myLib.set1-myLib.set1000 open=(defer) indsname=_ds_;
dsn = _ds_;
run;

proc sql;
create table want as
select 
    dsn,
    YEAR,
    sum(SALE*anyalpha(first(ID))) as SALE1,
    sum(SALE) as TOTALSALE
from allMyData
group by dsn, YEAR;
quit;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 01 Mar 2018 04:06:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-sum-variable-based-on-other-variables-for-1000-dataset/m-p/441112#M110302</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-03-01T04:06:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to sum variable based on other variables for 1000 dataset using MACRO</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-sum-variable-based-on-other-variables-for-1000-dataset/m-p/441328#M110386</link>
      <description>&lt;P&gt;But my dataset name is not structured. They are named differently.&lt;/P&gt;</description>
      <pubDate>Thu, 01 Mar 2018 18:54:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-sum-variable-based-on-other-variables-for-1000-dataset/m-p/441328#M110386</guid>
      <dc:creator>yanshuai</dc:creator>
      <dc:date>2018-03-01T18:54:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to sum variable based on other variables for 1000 dataset using MACRO</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-sum-variable-based-on-other-variables-for-1000-dataset/m-p/441334#M110389</link>
      <description>&lt;P&gt;But anyways, I have figured it out!!! Thank you!&lt;/P&gt;&lt;P&gt;Next issue is how to add an indentifying variable for each dataset according to dataset name. I will post another question.&lt;/P&gt;</description>
      <pubDate>Thu, 01 Mar 2018 19:00:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-sum-variable-based-on-other-variables-for-1000-dataset/m-p/441334#M110389</guid>
      <dc:creator>yanshuai</dc:creator>
      <dc:date>2018-03-01T19:00:08Z</dc:date>
    </item>
    <item>
      <title>Re: How to sum variable based on other variables for 1000 dataset using MACRO</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-sum-variable-based-on-other-variables-for-1000-dataset/m-p/441335#M110390</link>
      <description>&lt;P&gt;Thanks a lot!&lt;/P&gt;&lt;P&gt;I have figured it out! The name is not structured.&lt;/P&gt;</description>
      <pubDate>Thu, 01 Mar 2018 19:01:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-sum-variable-based-on-other-variables-for-1000-dataset/m-p/441335#M110390</guid>
      <dc:creator>yanshuai</dc:creator>
      <dc:date>2018-03-01T19:01:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to sum variable based on other variables for 1000 dataset using MACRO</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-sum-variable-based-on-other-variables-for-1000-dataset/m-p/441337#M110391</link>
      <description>&lt;P&gt;&lt;EM&gt;"an indentifying variable for each dataset according to dataset name"&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Variable DSN already plays that role in my answer.&lt;/P&gt;</description>
      <pubDate>Thu, 01 Mar 2018 19:05:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-sum-variable-based-on-other-variables-for-1000-dataset/m-p/441337#M110391</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-03-01T19:05:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to sum variable based on other variables for 1000 dataset using MACRO</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-sum-variable-based-on-other-variables-for-1000-dataset/m-p/441338#M110392</link>
      <description>&lt;P&gt;Cool! Thanks! INDSNAME is the code. Thank you&lt;/P&gt;</description>
      <pubDate>Thu, 01 Mar 2018 19:12:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-sum-variable-based-on-other-variables-for-1000-dataset/m-p/441338#M110392</guid>
      <dc:creator>yanshuai</dc:creator>
      <dc:date>2018-03-01T19:12:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to sum variable based on other variables for 1000 dataset using MACRO</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-sum-variable-based-on-other-variables-for-1000-dataset/m-p/441367#M110401</link>
      <description>&lt;P&gt;Hello PG.&lt;/P&gt;&lt;P&gt;The INDSNAME=_ds_ i gives me the name variable with folder name.&lt;/P&gt;&lt;P&gt;How can I get rid of the folder name?&lt;/P&gt;</description>
      <pubDate>Thu, 01 Mar 2018 20:42:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-sum-variable-based-on-other-variables-for-1000-dataset/m-p/441367#M110401</guid>
      <dc:creator>yanshuai</dc:creator>
      <dc:date>2018-03-01T20:42:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to sum variable based on other variables for 1000 dataset using MACRO</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-sum-variable-based-on-other-variables-for-1000-dataset/m-p/441371#M110402</link>
      <description>&lt;P&gt;Replace&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;dsn &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; _ds_&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token punctuation"&gt;with&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;SPAN class="token punctuation"&gt;dsn = scan(_ds_, 2);&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token punctuation"&gt;Check out the definition of the scan function in the documentation.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Mar 2018 20:50:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-sum-variable-based-on-other-variables-for-1000-dataset/m-p/441371#M110402</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-03-01T20:50:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to sum variable based on other variables for 1000 dataset using MACRO</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-sum-variable-based-on-other-variables-for-1000-dataset/m-p/441399#M110422</link>
      <description>&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Thu, 01 Mar 2018 22:04:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-sum-variable-based-on-other-variables-for-1000-dataset/m-p/441399#M110422</guid>
      <dc:creator>yanshuai</dc:creator>
      <dc:date>2018-03-01T22:04:42Z</dc:date>
    </item>
  </channel>
</rss>

