<?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: Problem with creating a table in a macro using proc sql in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Problem-with-creating-a-table-in-a-macro-using-proc-sql/m-p/852494#M336973</link>
    <description>&lt;P&gt;The line&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;options mprint;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;should go at the top of your program, not at the bottom. Please run the code again after making this change.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please copy the log as text and paste it into the window that appears when you click on the &amp;lt;/&amp;gt; icon. Do not attach files. Thank you.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Insert Log Icon in SAS Communities.png" style="width: 859px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/66171iFEC370B1DBF07B28/image-size/large?v=v2&amp;amp;px=999" role="button" title="Insert Log Icon in SAS Communities.png" alt="Insert Log Icon in SAS Communities.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 06 Jan 2023 13:28:52 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2023-01-06T13:28:52Z</dc:date>
    <item>
      <title>Problem with creating a table in a macro using proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problem-with-creating-a-table-in-a-macro-using-proc-sql/m-p/852478#M336969</link>
      <description>&lt;P&gt;Hi! I have a problem with a proc sql inside a macro. The log says that the work.segment6.data does not exist while I am creating it. Any ideas?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro rollsic_new;&lt;/P&gt;&lt;P&gt;%do i=1984 %to 2015;&lt;/P&gt;&lt;P&gt;%let lbound = i-4;&lt;BR /&gt;%let ubound = i;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table segments6(where=(sics ne . and share ge 0)) as&lt;BR /&gt;select distinct gvkey, input(sics,12.) as sics, mean(share) as share&lt;BR /&gt;from segments5&lt;BR /&gt;where year(datadate) between %eval(&amp;amp;lbound) and %eval(&amp;amp;ubound)&lt;BR /&gt;group by gvkey, sics;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql; select min(sics) into:minsics from segments6; quit;&lt;BR /&gt;proc sql; select max(sics) into:maxsics from segments6; quit;&lt;/P&gt;&lt;P&gt;* I select all distinct firms;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table du as&lt;BR /&gt;select distinct gvkey&lt;BR /&gt;from segments6(keep=gvkey);&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;*Eliminamos duplicados. OUTPUT: vector du (gvkeys de segments6 sin duplicados);&lt;/P&gt;&lt;P&gt;* Now select all segments present in that sample;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table sics as&lt;BR /&gt;select distinct sics&lt;BR /&gt;from segments6(keep=sics)&lt;BR /&gt;order by sics;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;*Eliminamos los sics duplicados de segments6 y ordenamos por sics;&lt;BR /&gt;*OUTPUT: vector sics (gvkeys de segments6 sin duplicados);&lt;/P&gt;&lt;P&gt;* Now I append all segments to each firm;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table both as&lt;BR /&gt;select a.gvkey, b.sics&lt;BR /&gt;from du as a, sics as b&lt;BR /&gt;order by a.gvkey, b.sics;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;*unimos los vectores de gvkeys y sics, ordenamos por gvkey y sics (asignamos gvkeys a sics);&lt;BR /&gt;*OUTPUT: base both;&lt;/P&gt;&lt;P&gt;* Now paste the segments6 table to have all info;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table both1 as&lt;BR /&gt;select a.*, b.share&lt;BR /&gt;from both as a left join segments6 as b&lt;BR /&gt;on a.gvkey = b.gvkey and a.sics = b.sics;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;*unimos both con el share de segments6 para gvkey y sics iguales;&lt;BR /&gt;*mantenemos todas las variables de both y solo share de segments6;&lt;BR /&gt;*OUTPUT: base both1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;* I transpose to have in columns the mgrnones;&lt;BR /&gt;proc transpose data=both1 out=wseg1(drop=NAME) prefix=sics;&lt;BR /&gt;by gvkey;&lt;BR /&gt;id sics;&lt;BR /&gt;var share;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;*corregimos el drop=NAME por drop=NAME;&lt;BR /&gt;*De lo contrario, se mantenia la columna de NAME con datapoints de "share";&lt;BR /&gt;*Creamos matriz traspuesta: columnas contiene los sics y filas gvkeys;&lt;BR /&gt;*Data points son los share;&lt;/P&gt;&lt;P&gt;*;&lt;/P&gt;&lt;P&gt;* Now I calculate the cosine similarity;&lt;BR /&gt;proc distance data=wseg1 out=segCos method=COSINE shape=square replace;&lt;BR /&gt;var ratio(sics%eval(&amp;amp;minsics)--sics%eval(&amp;amp;maxsics));&lt;BR /&gt;id gvkey;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;*Creamos matriz cuadrada que contiene gvkeys en filas y columnas;&lt;BR /&gt;*Con cosine similarity en los datapoints;&lt;/P&gt;&lt;P&gt;* I transpose the resulting matrix to make it accessible;&lt;BR /&gt;proc sort data=segcos; by gvkey; run;&lt;BR /&gt;*ordenamos segcos por gvkeys;&lt;/P&gt;&lt;P&gt;proc transpose data=segcos out=segg;&lt;BR /&gt;by gvkey;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;*Panel que contiene panel de gvkeys con su respectivo cosine similarity;&lt;/P&gt;&lt;P&gt;* And put it nicely so I have the main variables and the cosine similarty called t;&lt;BR /&gt;data sic&amp;amp;i(drop=NAME gvkey rename=(COL1=sic));&lt;BR /&gt;retain gvkey1 gvkey2;&lt;BR /&gt;set segg;&lt;BR /&gt;year = &amp;amp;i;&lt;BR /&gt;gvkey1=input(gvkey,12.);&lt;BR /&gt;gvkey2=input(substr(NAME,2),12.);&lt;BR /&gt;where input(gvkey,12.) ne input(substr(NAME,2),12.) and COL1 not in (. 0);&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%end;&lt;BR /&gt;%mend;&lt;BR /&gt;%rollsic_new;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Jan 2023 12:04:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problem-with-creating-a-table-in-a-macro-using-proc-sql/m-p/852478#M336969</guid>
      <dc:creator>Ramiro_iese</dc:creator>
      <dc:date>2023-01-06T12:04:25Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with creating a table in a macro using proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problem-with-creating-a-table-in-a-macro-using-proc-sql/m-p/852480#M336970</link>
      <description>&lt;P&gt;If the log has detected problems, show us the log. Show us the ENTIRE log for one loop of the macro (so change the code to read &lt;SPAN&gt;&lt;FONT face="courier new,courier"&gt;%do i=1984 %to 1984;&lt;/FONT&gt;), we need to see&lt;/SPAN&gt;&amp;nbsp;all of the log, every single line, every single character. Since this is a macro, you also need to turn on debugging options by running this line of code, then re-running the macro, and then showing us the ENTIRE log.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options mprint;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please paste the log into the window that appears when you click on the &amp;lt;/&amp;gt; icon&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Insert Log Icon in SAS Communities.png" style="width: 859px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/66171iFEC370B1DBF07B28/image-size/large?v=v2&amp;amp;px=999" role="button" title="Insert Log Icon in SAS Communities.png" alt="Insert Log Icon in SAS Communities.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Jan 2023 12:17:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problem-with-creating-a-table-in-a-macro-using-proc-sql/m-p/852480#M336970</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-01-06T12:17:20Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with creating a table in a macro using proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problem-with-creating-a-table-in-a-macro-using-proc-sql/m-p/852487#M336972</link>
      <description>&lt;P&gt;Thank you for your response! here is the log for one iteration. The macro is in line 526.&lt;/P&gt;&lt;P&gt;Thank you again!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Jan 2023 13:04:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problem-with-creating-a-table-in-a-macro-using-proc-sql/m-p/852487#M336972</guid>
      <dc:creator>Ramiro_iese</dc:creator>
      <dc:date>2023-01-06T13:04:06Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with creating a table in a macro using proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problem-with-creating-a-table-in-a-macro-using-proc-sql/m-p/852494#M336973</link>
      <description>&lt;P&gt;The line&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;options mprint;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;should go at the top of your program, not at the bottom. Please run the code again after making this change.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please copy the log as text and paste it into the window that appears when you click on the &amp;lt;/&amp;gt; icon. Do not attach files. Thank you.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Insert Log Icon in SAS Communities.png" style="width: 859px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/66171iFEC370B1DBF07B28/image-size/large?v=v2&amp;amp;px=999" role="button" title="Insert Log Icon in SAS Communities.png" alt="Insert Log Icon in SAS Communities.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Jan 2023 13:28:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problem-with-creating-a-table-in-a-macro-using-proc-sql/m-p/852494#M336973</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-01-06T13:28:52Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with creating a table in a macro using proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problem-with-creating-a-table-in-a-macro-using-proc-sql/m-p/852495#M336974</link>
      <description>&lt;P&gt;When debugging, SAS, you should review the log an look for the first error (or bad warning/note).&amp;nbsp; In this case, the error message is immediately after the macro call:&lt;/P&gt;
&lt;PRE&gt;649  %rollsic_new;
ERROR: A character operand was found in the %EVAL function or
       %IF condition where a numeric operand is required. The
       condition was: i-4
ERROR: A character operand was found in the %EVAL function or
       %IF condition where a numeric operand is required. The
       condition was: i
&lt;/PRE&gt;
&lt;P&gt;This message is saying that the macro language is trying to do integer arithmetic with a value, but the value is character.&amp;nbsp; If you look at the start of your macro definition, you have:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let lbound = i-4;
%let ubound = i;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This will give lbound the value i-4, meaning it a a text value three characters long. Since you intend i to be a macro variable reference, you should change to:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let lbound = %eval(&amp;amp;i-4) ;
%let ubound = &amp;amp;i ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that I used the %eval function to tell the macro processer to do subtraction.&amp;nbsp; Without using %eval, your code might work, but the value of lbound would be for example 1984-4, rather than 1980.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another tip is to turn on the system option MPRINT, so that the log will show you the SAS code generated by your macro.&amp;nbsp; It's very difficult to debug macros without the MPRINT option turned on.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Jan 2023 13:33:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problem-with-creating-a-table-in-a-macro-using-proc-sql/m-p/852495#M336974</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-01-06T13:33:39Z</dc:date>
    </item>
  </channel>
</rss>

