<?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: running macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/running-macro/m-p/892589#M352538</link>
    <description>&lt;P&gt;When trying to understand what a macro is doing, sometimes it's helpful to de-macrify the code, so you can run it in steps and inspect.&amp;nbsp; I think the below code will produce your d2 dataset, with two years of data.&amp;nbsp; You didn't show the value of &amp;amp;lvar, so I left that macro reference in place:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data bv_1990; set d1; if FYEAR=1990; proc sort data=bv_1990 nouniquekey uniqueout=bvu_1990; by ID;
data bv_1990; set d1; if FYEAR=1990; proc sort data=bv_1990 nouniquekey out=bvm_1990; by ID;
proc summary data=bvm_1990 nway; class ID; var &amp;amp;lvar.; id FYEAR; output out=bvm_1990 max=;


data bv_1991; set d1; if FYEAR=1991; proc sort data=bv_1991 nouniquekey uniqueout=bvu_1991; by ID;
data bv_1991; set d1; if FYEAR=1991; proc sort data=bv_1991 nouniquekey out=bvm_1991; by ID;
proc summary data=bvm_1991 nway; class ID; var &amp;amp;lvar.; id FYEAR; output out=bvm_1991 max=;

data d2; set bvu_1990-bvu_1991 bvm_1990-bvm_1991; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Looking at that, the code is confusing to me.&amp;nbsp; bvu_1990 will have records from D1 with FYEAR=1990 that have unique values for ID.&amp;nbsp; It could have variables that are not listed in &amp;amp;lvar.&amp;nbsp; bvm_1990 will only have variables that are listed in &amp;amp;lvar.&amp;nbsp; It's odd to stack the original data and the MAX data back together in this way.&amp;nbsp; It's possible all of this could be done without a macro, by adding FYEAR to the BY statement and CLASS statement.&amp;nbsp; Or perhaps in single PROC SQL step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would suggest trying to trace/debug the code without the macro.&amp;nbsp; Then once you have non-macro code working like you want, you can think about how to update the macro.&lt;/P&gt;</description>
    <pubDate>Mon, 04 Sep 2023 16:11:41 GMT</pubDate>
    <dc:creator>Quentin</dc:creator>
    <dc:date>2023-09-04T16:11:41Z</dc:date>
    <item>
      <title>running macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/running-macro/m-p/892565#M352524</link>
      <description>&lt;P&gt;I'm running the macro below, where lvar is a list of financial variables. There are other variables in the datasets. At some point in the macro I keep the max of multiple entries for the financial variables (lvar). I would like to know what SAS does with the non-financial variables: which one is kept in the case of multiple entries? How can I control this part of the process?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;option mprint symbolgen;
%macro cc(dsin,dsout);
%do y=1990 %to 2022;

data bv_&amp;amp;y; set &amp;amp;dsin; if FYEAR=&amp;amp;y; proc sort data=bv_&amp;amp;y nouniquekey uniqueout=bvu_&amp;amp;y; by ID;
data bv_&amp;amp;y; set &amp;amp;dsin; if FYEAR=&amp;amp;y; proc sort data=bv_&amp;amp;y nouniquekey out=bvm_&amp;amp;y; by ID;
proc summary data=bvm_&amp;amp;y nway; class ID; var &amp;amp;lvar.; id FYEAR; output out=bvm_&amp;amp;y max=;
%end;

data &amp;amp;dsout; set bvu_1990-bvu_2022 bvm_1990-bvm_2022; run;
data &amp;amp;dsout; set &amp;amp;dsout; drop _FREQ_ _TYPE_ i j;
%mend;

%cc(d1,d2)
%cc(c1,c2)
option nomprint nosymbolgen;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Sep 2023 22:32:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/running-macro/m-p/892565#M352524</guid>
      <dc:creator>Satori</dc:creator>
      <dc:date>2023-09-04T22:32:16Z</dc:date>
    </item>
    <item>
      <title>Re: running macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/running-macro/m-p/892578#M352536</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/173636"&gt;@Satori&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I'm running the macro below, where lvar is a list of financial variables. There are other variables in the datasets. At some point in the macro I keep the max of multiple entries for the financial variables (lvar). I would like to know what SAS does with the non-financial variables: which one is kept in the case of multiple entries? How can I control this part of the process?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;UL&gt;
&lt;LI&gt;Can you provide a portion of the data as WORKING data step code?&lt;/LI&gt;
&lt;LI&gt;Can you be specific about what you are talking about in your last two sentences? What non-financial variables? What do you mean "which one is kept"? Kept by what? Control so that what happens?&lt;/LI&gt;
&lt;/UL&gt;</description>
      <pubDate>Mon, 04 Sep 2023 15:23:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/running-macro/m-p/892578#M352536</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-09-04T15:23:11Z</dc:date>
    </item>
    <item>
      <title>Re: running macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/running-macro/m-p/892589#M352538</link>
      <description>&lt;P&gt;When trying to understand what a macro is doing, sometimes it's helpful to de-macrify the code, so you can run it in steps and inspect.&amp;nbsp; I think the below code will produce your d2 dataset, with two years of data.&amp;nbsp; You didn't show the value of &amp;amp;lvar, so I left that macro reference in place:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data bv_1990; set d1; if FYEAR=1990; proc sort data=bv_1990 nouniquekey uniqueout=bvu_1990; by ID;
data bv_1990; set d1; if FYEAR=1990; proc sort data=bv_1990 nouniquekey out=bvm_1990; by ID;
proc summary data=bvm_1990 nway; class ID; var &amp;amp;lvar.; id FYEAR; output out=bvm_1990 max=;


data bv_1991; set d1; if FYEAR=1991; proc sort data=bv_1991 nouniquekey uniqueout=bvu_1991; by ID;
data bv_1991; set d1; if FYEAR=1991; proc sort data=bv_1991 nouniquekey out=bvm_1991; by ID;
proc summary data=bvm_1991 nway; class ID; var &amp;amp;lvar.; id FYEAR; output out=bvm_1991 max=;

data d2; set bvu_1990-bvu_1991 bvm_1990-bvm_1991; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Looking at that, the code is confusing to me.&amp;nbsp; bvu_1990 will have records from D1 with FYEAR=1990 that have unique values for ID.&amp;nbsp; It could have variables that are not listed in &amp;amp;lvar.&amp;nbsp; bvm_1990 will only have variables that are listed in &amp;amp;lvar.&amp;nbsp; It's odd to stack the original data and the MAX data back together in this way.&amp;nbsp; It's possible all of this could be done without a macro, by adding FYEAR to the BY statement and CLASS statement.&amp;nbsp; Or perhaps in single PROC SQL step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would suggest trying to trace/debug the code without the macro.&amp;nbsp; Then once you have non-macro code working like you want, you can think about how to update the macro.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Sep 2023 16:11:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/running-macro/m-p/892589#M352538</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-09-04T16:11:41Z</dc:date>
    </item>
    <item>
      <title>Re: running macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/running-macro/m-p/892614#M352554</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/173636"&gt;@Satori&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I'm running the macro below, where lvar is a list of financial variables. There are other variables in the datasets. At some point in the macro I keep the max of multiple entries for the financial variables (lvar). I would like to know what SAS does with the non-financial variables: which one is kept in the case of multiple entries? How can I control this part of the process?&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;At which step???&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Data steps will have all variables provided in data sets on a SET statement and created in the course of the data step unless they are explicitly dropped somewhere.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Proc Summary will generally only have variables used or created by the proc. If you expect "other" variables to appear in Proc Summary output you will have to provide a very explicit example with input data and what you expect the output to look like.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I will say that I suspect you have a poor data structure as you have different variables with the year in the name. Often it is better in the long run to have a variable with the Year value and all variables to be the same name.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This apparent process with a different data structure would likely remove any need for looping and likely any need for a macro at all. Just use the Year variable as one of the BY or Class variables.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Sep 2023 17:57:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/running-macro/m-p/892614#M352554</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-09-04T17:57:58Z</dc:date>
    </item>
  </channel>
</rss>

