<?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: Variable increment based on Macro variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Variable-increment-based-on-Macro-variable/m-p/857684#M338893</link>
    <description>&lt;P&gt;Thanks for the reply Tom!&lt;/P&gt;
&lt;P&gt;&amp;amp;max_pk1 has numeric data 15.&lt;/P&gt;
&lt;P&gt;Expected output;&lt;/P&gt;
&lt;TABLE style="border-collapse: collapse; width: 96pt;" border="0" width="128" cellspacing="0" cellpadding="0"&gt;
&lt;TBODY&gt;
&lt;TR style="height: 14.5pt;"&gt;
&lt;TD width="64" height="19" class="xl63" style="height: 14.5pt; width: 48pt;"&gt;ISSUE&lt;/TD&gt;
&lt;TD width="64" class="xl63" style="border-left: none; width: 48pt;"&gt;PK&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.5pt;"&gt;
&lt;TD height="19" class="xl63" style="height: 14.5pt; border-top: none;"&gt;ABC1&lt;/TD&gt;
&lt;TD class="xl63" style="border-top: none; border-left: none;"&gt;16&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.5pt;"&gt;
&lt;TD height="19" class="xl63" style="height: 14.5pt; border-top: none;"&gt;ABC2&lt;/TD&gt;
&lt;TD class="xl63" style="border-top: none; border-left: none;"&gt;17&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.5pt;"&gt;
&lt;TD height="19" class="xl63" style="height: 14.5pt; border-top: none;"&gt;ABC3&lt;/TD&gt;
&lt;TD class="xl63" style="border-top: none; border-left: none;"&gt;18&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.5pt;"&gt;
&lt;TD height="19" class="xl63" style="height: 14.5pt; border-top: none;"&gt;ABC4&lt;/TD&gt;
&lt;TD class="xl63" style="border-top: none; border-left: none;"&gt;19&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So whenever VARIABLE 'ISSUE' IS UNIQUE, THE PK variable should increment by 1. I'm taking the inital value as 15 so it should be populated as 15,16,17,18,19,.....&lt;/P&gt;</description>
    <pubDate>Wed, 08 Feb 2023 01:31:59 GMT</pubDate>
    <dc:creator>Banana19</dc:creator>
    <dc:date>2023-02-08T01:31:59Z</dc:date>
    <item>
      <title>Variable increment based on Macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Variable-increment-based-on-Macro-variable/m-p/857681#M338890</link>
      <description>&lt;P&gt;Hi all, I'm trying to increment a variable based on a macro variable. I tried to use the below code,&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SORT DATA=WORK.tab1;
BY ISSUE;
RUN;

DATA tab2;
SET WORK.tab1;
BY ISSUE;
RETAIN PK &amp;amp;MAX_PK1.;
IF FIRST.ISSUE THEN PK = &amp;amp;MAX_PK1. + 1;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;but it generates same number across every row. Could you please let me know what am I missing?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;Banana&lt;/P&gt;</description>
      <pubDate>Wed, 08 Feb 2023 00:24:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Variable-increment-based-on-Macro-variable/m-p/857681#M338890</guid>
      <dc:creator>Banana19</dc:creator>
      <dc:date>2023-02-08T00:24:58Z</dc:date>
    </item>
    <item>
      <title>Re: Variable increment based on Macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Variable-increment-based-on-Macro-variable/m-p/857683#M338892</link>
      <description>&lt;P&gt;In that code you are appear to be using the macro variable MAX_PK1 as if it contained the NAME of a variable.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example if MAX_PK1 has the value VAR1, like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let max_pk1=VAR1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then the data step your macro logic generates would be:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA tab2;
SET WORK.tab1;
BY ISSUE;
RETAIN PK VAR1;
IF FIRST.ISSUE THEN PK = VAR1 + 1;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If instead you have set MAX_PK1 to an actual number, like 100,&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let max_pk1=100;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;then you would not want to use it in the RETAIN statement so that you could generate code like this instead.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA tab2;
SET WORK.tab1;
BY ISSUE;
RETAIN PK ;
IF FIRST.ISSUE THEN PK = 100 + 1;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Either way it is obvious why PK is set to a constant. It is only set to a value on the first observation for each ISSUE group. And the value it is set to is always the same.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please show sample simple example input and expected output.&amp;nbsp; You only need about two or three observations per ISSUE value for two or three different values of ISSUE.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Feb 2023 01:15:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Variable-increment-based-on-Macro-variable/m-p/857683#M338892</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-02-08T01:15:56Z</dc:date>
    </item>
    <item>
      <title>Re: Variable increment based on Macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Variable-increment-based-on-Macro-variable/m-p/857684#M338893</link>
      <description>&lt;P&gt;Thanks for the reply Tom!&lt;/P&gt;
&lt;P&gt;&amp;amp;max_pk1 has numeric data 15.&lt;/P&gt;
&lt;P&gt;Expected output;&lt;/P&gt;
&lt;TABLE style="border-collapse: collapse; width: 96pt;" border="0" width="128" cellspacing="0" cellpadding="0"&gt;
&lt;TBODY&gt;
&lt;TR style="height: 14.5pt;"&gt;
&lt;TD width="64" height="19" class="xl63" style="height: 14.5pt; width: 48pt;"&gt;ISSUE&lt;/TD&gt;
&lt;TD width="64" class="xl63" style="border-left: none; width: 48pt;"&gt;PK&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.5pt;"&gt;
&lt;TD height="19" class="xl63" style="height: 14.5pt; border-top: none;"&gt;ABC1&lt;/TD&gt;
&lt;TD class="xl63" style="border-top: none; border-left: none;"&gt;16&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.5pt;"&gt;
&lt;TD height="19" class="xl63" style="height: 14.5pt; border-top: none;"&gt;ABC2&lt;/TD&gt;
&lt;TD class="xl63" style="border-top: none; border-left: none;"&gt;17&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.5pt;"&gt;
&lt;TD height="19" class="xl63" style="height: 14.5pt; border-top: none;"&gt;ABC3&lt;/TD&gt;
&lt;TD class="xl63" style="border-top: none; border-left: none;"&gt;18&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.5pt;"&gt;
&lt;TD height="19" class="xl63" style="height: 14.5pt; border-top: none;"&gt;ABC4&lt;/TD&gt;
&lt;TD class="xl63" style="border-top: none; border-left: none;"&gt;19&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So whenever VARIABLE 'ISSUE' IS UNIQUE, THE PK variable should increment by 1. I'm taking the inital value as 15 so it should be populated as 15,16,17,18,19,.....&lt;/P&gt;</description>
      <pubDate>Wed, 08 Feb 2023 01:31:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Variable-increment-based-on-Macro-variable/m-p/857684#M338893</guid>
      <dc:creator>Banana19</dc:creator>
      <dc:date>2023-02-08T01:31:59Z</dc:date>
    </item>
    <item>
      <title>Re: Variable increment based on Macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Variable-increment-based-on-Macro-variable/m-p/857686#M338895</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/89915"&gt;@Banana19&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks for the reply Tom!&lt;/P&gt;
&lt;P&gt;&amp;amp;max_pk1 has numeric data 15.&lt;/P&gt;
&lt;P&gt;Expected output;&lt;/P&gt;
&lt;TABLE style="border-collapse: collapse; width: 96pt;" border="0" width="128" cellspacing="0" cellpadding="0"&gt;
&lt;TBODY&gt;
&lt;TR style="height: 14.5pt;"&gt;
&lt;TD width="64" height="19" class="xl63" style="height: 14.5pt; width: 48pt;"&gt;ISSUE&lt;/TD&gt;
&lt;TD width="64" class="xl63" style="border-left: none; width: 48pt;"&gt;PK&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.5pt;"&gt;
&lt;TD height="19" class="xl63" style="height: 14.5pt; border-top: none;"&gt;ABC1&lt;/TD&gt;
&lt;TD class="xl63" style="border-top: none; border-left: none;"&gt;16&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.5pt;"&gt;
&lt;TD height="19" class="xl63" style="height: 14.5pt; border-top: none;"&gt;ABC2&lt;/TD&gt;
&lt;TD class="xl63" style="border-top: none; border-left: none;"&gt;17&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.5pt;"&gt;
&lt;TD height="19" class="xl63" style="height: 14.5pt; border-top: none;"&gt;ABC3&lt;/TD&gt;
&lt;TD class="xl63" style="border-top: none; border-left: none;"&gt;18&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.5pt;"&gt;
&lt;TD height="19" class="xl63" style="height: 14.5pt; border-top: none;"&gt;ABC4&lt;/TD&gt;
&lt;TD class="xl63" style="border-top: none; border-left: none;"&gt;19&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So whenever VARIABLE 'ISSUE' IS UNIQUE, THE PK variable should increment by 1. I'm taking the inital value as 15 so it should be populated as 15,16,17,18,19,.....&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So before you try to use macro logic to generate SAS code you need to know what SAS code you need to generate.&amp;nbsp; To do what you ask you just need code like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by issue;
  retain pk 15 ;
  if first.issue and _n_&amp;gt;1 then pk+1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To make it dynamic just replace the 15 with the macro variable reference.&lt;/P&gt;</description>
      <pubDate>Wed, 08 Feb 2023 03:54:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Variable-increment-based-on-Macro-variable/m-p/857686#M338895</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-02-08T03:54:05Z</dc:date>
    </item>
    <item>
      <title>Re: Variable increment based on Macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Variable-increment-based-on-Macro-variable/m-p/857747#M338926</link>
      <description>Thank you,Tom! It worked!!</description>
      <pubDate>Wed, 08 Feb 2023 13:37:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Variable-increment-based-on-Macro-variable/m-p/857747#M338926</guid>
      <dc:creator>Banana19</dc:creator>
      <dc:date>2023-02-08T13:37:19Z</dc:date>
    </item>
  </channel>
</rss>

