<?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: reassign macro variable at first instance of by variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/reassign-macro-variable-at-first-instance-of-by-variable/m-p/346208#M79799</link>
    <description>&lt;P&gt;Why would it change? &amp;nbsp;When SAS compiles the data step it will use the current value of the macro variable. So if it was defined as 5 before SAS started to compile the data step then the code would look like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let lastlot=5 ;

data new ;
  set have ;
  by id ;
  retain n ;
  if first.id = 1 then do;
    call symputx('lastlot',trunclot) ;
    n=5 ;
  end;
  DO i = 1 TO n ;
      ...
  end;
run;

 &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It looks like you want to do:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data new ;
  set have ;
  by id ;
  retain n ;
  if first.id then n = trunclot ;
  DO i = 1 TO n ;
      ...
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 31 Mar 2017 16:33:54 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2017-03-31T16:33:54Z</dc:date>
    <item>
      <title>reassign macro variable at first instance of by variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/reassign-macro-variable-at-first-instance-of-by-variable/m-p/346203#M79796</link>
      <description>&lt;P&gt;The "n" below is not changing at the start of each new ID. &amp;nbsp;Can macro variables like this be overwritten with each new instance of a by variable?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data new ;&lt;/P&gt;&lt;P&gt;set have ;&lt;/P&gt;&lt;P&gt;by id ;&lt;/P&gt;&lt;P&gt;retain n ;&lt;/P&gt;&lt;P&gt;if first.id = 1 then do;&lt;BR /&gt;call symputx('lastlot',trunclot) ;&lt;BR /&gt;n=&amp;amp;lastlot. ;&lt;BR /&gt;end;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;DO i = 1 TO n ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;etc.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 31 Mar 2017 16:17:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/reassign-macro-variable-at-first-instance-of-by-variable/m-p/346203#M79796</guid>
      <dc:creator>CP2</dc:creator>
      <dc:date>2017-03-31T16:17:37Z</dc:date>
    </item>
    <item>
      <title>Re: reassign macro variable at first instance of by variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/reassign-macro-variable-at-first-instance-of-by-variable/m-p/346208#M79799</link>
      <description>&lt;P&gt;Why would it change? &amp;nbsp;When SAS compiles the data step it will use the current value of the macro variable. So if it was defined as 5 before SAS started to compile the data step then the code would look like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let lastlot=5 ;

data new ;
  set have ;
  by id ;
  retain n ;
  if first.id = 1 then do;
    call symputx('lastlot',trunclot) ;
    n=5 ;
  end;
  DO i = 1 TO n ;
      ...
  end;
run;

 &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It looks like you want to do:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data new ;
  set have ;
  by id ;
  retain n ;
  if first.id then n = trunclot ;
  DO i = 1 TO n ;
      ...
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 31 Mar 2017 16:33:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/reassign-macro-variable-at-first-instance-of-by-variable/m-p/346208#M79799</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-03-31T16:33:54Z</dc:date>
    </item>
    <item>
      <title>Re: reassign macro variable at first instance of by variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/reassign-macro-variable-at-first-instance-of-by-variable/m-p/346210#M79801</link>
      <description>&lt;P&gt;Yes the second is what I want. I have a complicated piece of code to work out so I was over complicating this simple task. Thanks for your help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 31 Mar 2017 16:38:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/reassign-macro-variable-at-first-instance-of-by-variable/m-p/346210#M79801</guid>
      <dc:creator>CP2</dc:creator>
      <dc:date>2017-03-31T16:38:22Z</dc:date>
    </item>
  </channel>
</rss>

