<?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 performing an operation to a list of variables inside a table in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/performing-an-operation-to-a-list-of-variables-inside-a-table/m-p/311308#M67245</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a table of historical stock prices, among other things, where each stock is in a column. I also have all the names of the stocks in a macro list called "symbol".&lt;/P&gt;
&lt;P&gt;I would like to calculate the return for each stock. I have tried the following code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro returns(all_data);
%let k=1;

    %let symbol= %scan(&amp;amp;all_data, &amp;amp;k);
%do %while("&amp;amp;symbol" NE "");
&amp;amp;symbol&amp;amp;_&amp;amp;ret = log(&amp;amp;symbol) - log(lag(&amp;amp;symbol));
%let k = %eval(&amp;amp;k + 1);
    %let symbol = %scan(&amp;amp;all_data, &amp;amp;k);

%end;
%mend;

data returns;
set returns;
%returns(&amp;amp;symbol);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But the code doesn't work. I don't get any error message, but the new stock return columns don't appear.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 14 Nov 2016 03:23:39 GMT</pubDate>
    <dc:creator>ilikesas</dc:creator>
    <dc:date>2016-11-14T03:23:39Z</dc:date>
    <item>
      <title>performing an operation to a list of variables inside a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/performing-an-operation-to-a-list-of-variables-inside-a-table/m-p/311308#M67245</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a table of historical stock prices, among other things, where each stock is in a column. I also have all the names of the stocks in a macro list called "symbol".&lt;/P&gt;
&lt;P&gt;I would like to calculate the return for each stock. I have tried the following code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro returns(all_data);
%let k=1;

    %let symbol= %scan(&amp;amp;all_data, &amp;amp;k);
%do %while("&amp;amp;symbol" NE "");
&amp;amp;symbol&amp;amp;_&amp;amp;ret = log(&amp;amp;symbol) - log(lag(&amp;amp;symbol));
%let k = %eval(&amp;amp;k + 1);
    %let symbol = %scan(&amp;amp;all_data, &amp;amp;k);

%end;
%mend;

data returns;
set returns;
%returns(&amp;amp;symbol);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But the code doesn't work. I don't get any error message, but the new stock return columns don't appear.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Nov 2016 03:23:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/performing-an-operation-to-a-list-of-variables-inside-a-table/m-p/311308#M67245</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2016-11-14T03:23:39Z</dc:date>
    </item>
    <item>
      <title>Re: performing an operation to a list of variables inside a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/performing-an-operation-to-a-list-of-variables-inside-a-table/m-p/311315#M67249</link>
      <description>&lt;P&gt;Don't use a macro, use an array or transpose your data and use BY group processing.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://www.ats.ucla.edu/stat/sas/seminars/SAS_arrays/" target="_blank"&gt;http://www.ats.ucla.edu/stat/sas/seminars/SAS_arrays/&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Nov 2016 03:59:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/performing-an-operation-to-a-list-of-variables-inside-a-table/m-p/311315#M67249</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-11-14T03:59:22Z</dc:date>
    </item>
    <item>
      <title>Re: performing an operation to a list of variables inside a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/performing-an-operation-to-a-list-of-variables-inside-a-table/m-p/311318#M67251</link>
      <description>&lt;P&gt;I was actually trying to put a list into an array with the following code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data returns (drop = i );
   set returns;
   array returns{*} &amp;amp;symbol;
   do i =1 to dim(returns);
      returns[i]_ret = log(returns[i])- log(lag(returns[i] ))
   end;
   
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I know that I can write manually all the stock symbols into the array but I have too many&amp;nbsp;stocks...&lt;/P&gt;
&lt;P&gt;That is why here I tried to put the list into the array. I was not surprised when the code didn't work, but is it even possible?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Mon, 14 Nov 2016 04:11:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/performing-an-operation-to-a-list-of-variables-inside-a-table/m-p/311318#M67251</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2016-11-14T04:11:04Z</dc:date>
    </item>
    <item>
      <title>Re: performing an operation to a list of variables inside a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/performing-an-operation-to-a-list-of-variables-inside-a-table/m-p/311319#M67252</link>
      <description>&lt;P&gt;You need two arrays. See the 'COMPUTING NEW VARIABLES' example in the link I provided.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Depending on how you created your variable list creating a second variable list with the suffix ret can be easy or hard.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Nov 2016 04:34:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/performing-an-operation-to-a-list-of-variables-inside-a-table/m-p/311319#M67252</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-11-14T04:34:43Z</dc:date>
    </item>
    <item>
      <title>Re: performing an operation to a list of variables inside a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/performing-an-operation-to-a-list-of-variables-inside-a-table/m-p/311320#M67253</link>
      <description>&lt;P&gt;But in this case I still need to manually enter all the names of the stocks?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Nov 2016 04:54:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/performing-an-operation-to-a-list-of-variables-inside-a-table/m-p/311320#M67253</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2016-11-14T04:54:30Z</dc:date>
    </item>
    <item>
      <title>Re: performing an operation to a list of variables inside a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/performing-an-operation-to-a-list-of-variables-inside-a-table/m-p/311321#M67254</link>
      <description>&lt;P&gt;Not unless you want to.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You said they were stored in STOCKS macro variable. How did you create that list? Modify to create a second list of variables that the required suffix and call it something like stocks_lag.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Nov 2016 05:01:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/performing-an-operation-to-a-list-of-variables-inside-a-table/m-p/311321#M67254</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-11-14T05:01:59Z</dc:date>
    </item>
    <item>
      <title>Re: performing an operation to a list of variables inside a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/performing-an-operation-to-a-list-of-variables-inside-a-table/m-p/311323#M67256</link>
      <description>&lt;P&gt;OK I think I got it:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1) first I added a new column to create a stock_ret:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data the_stocks;&lt;BR /&gt;set the_stocks;&lt;BR /&gt;returns = cats(symbol,"_ret");&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2) Then I created a list for the stock_ret:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql noprint ;&lt;BR /&gt; select returns&lt;BR /&gt; into :returns separated by ' '&lt;BR /&gt; from the_stocks;&lt;BR /&gt; quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3) And then I used the arrays to calculate the stock returns:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data returns (drop= i);&lt;BR /&gt;set returns;&lt;BR /&gt; array returns{*} &amp;amp;returns;&lt;BR /&gt;array symbol{*} &amp;amp;symbol;&lt;BR /&gt; do i =1 to dim(returns);&lt;BR /&gt;returns[i] = log(symbol[i])- log(lag(symbol[i]));&lt;BR /&gt; end;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Nov 2016 06:01:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/performing-an-operation-to-a-list-of-variables-inside-a-table/m-p/311323#M67256</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2016-11-14T06:01:50Z</dc:date>
    </item>
    <item>
      <title>Re: performing an operation to a list of variables inside a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/performing-an-operation-to-a-list-of-variables-inside-a-table/m-p/311324#M67257</link>
      <description>&lt;P&gt;Looks good!&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Only suggestion is you can create list directly from stock list and add an order by to be dead certain the observations are in the same order.&amp;nbsp;&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;&lt;/P&gt;
&lt;P&gt;select catx('_', symbol, 'ret') into :returns separated by ' '&amp;nbsp;&lt;/P&gt;
&lt;P&gt;from the_stocks&amp;nbsp;&lt;/P&gt;
&lt;P&gt;order by symbol;&lt;/P&gt;
&lt;P&gt;select symbol into :stocks separated by ' '&amp;nbsp;&lt;/P&gt;
&lt;P&gt;from the_stocks&lt;/P&gt;
&lt;P&gt;order by symbol;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;quit;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Nov 2016 06:08:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/performing-an-operation-to-a-list-of-variables-inside-a-table/m-p/311324#M67257</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-11-14T06:08:16Z</dc:date>
    </item>
    <item>
      <title>Re: performing an operation to a list of variables inside a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/performing-an-operation-to-a-list-of-variables-inside-a-table/m-p/311603#M67394</link>
      <description>&lt;P&gt;Thanks Reeza for the guidance, so I will summarize for future readers.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1) I have a table of the names of the stocks:&lt;/P&gt;
&lt;P&gt;data the_stocks;&lt;BR /&gt;input symbol$;&lt;BR /&gt;datalines;&lt;BR /&gt;msft&lt;BR /&gt;ko&lt;BR /&gt;ibm&lt;BR /&gt;duk&lt;BR /&gt;bp&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(And I also have a table of the stock returns where each stock price is in its own column)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2) Here create a list of the stocks and also a list of the stock returns in the form "stock_ret"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;Proc SQL noprint;&lt;BR /&gt;select catx('_', symbol, 'ret') into :returns separated by ' ' &lt;BR /&gt;from the_stocks ;&lt;BR /&gt;select symbol into :symbol separated by ' ' &lt;BR /&gt;from the_stocks;&lt;BR /&gt; quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3) And finally calculate the stock returns by putting both lists into arrays:&lt;/P&gt;
&lt;P&gt;data returns (drop= i);&lt;BR /&gt;set returns;&lt;BR /&gt; array returns{*} &amp;amp;returns;&lt;BR /&gt;array symbol{*} &amp;amp;symbol;&lt;BR /&gt; do i =1 to dim(returns);&lt;BR /&gt;returns[i] = log(symbol[i])- log(lag(symbol[i]));&lt;BR /&gt; end;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Nov 2016 04:31:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/performing-an-operation-to-a-list-of-variables-inside-a-table/m-p/311603#M67394</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2016-11-15T04:31:14Z</dc:date>
    </item>
  </channel>
</rss>

