<?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: Clearing a variable counter after the program executes in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Clearing-a-variable-counter-after-the-program-executes/m-p/627868#M185435</link>
    <description>&lt;P&gt;Oh yes, and you absolutely need to show us your original dataset (before you run this step). Post example data in a data&amp;nbsp; step with datalines (this is absolutely necessary!), use the "little running man" to post code.&lt;/P&gt;</description>
    <pubDate>Thu, 27 Feb 2020 12:04:09 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2020-02-27T12:04:09Z</dc:date>
    <item>
      <title>Clearing a variable counter after the program executes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Clearing-a-variable-counter-after-the-program-executes/m-p/627857#M185427</link>
      <description>&lt;P&gt;This code is supposed to count number of doctor visits per patient, which it does the first time it's executed. But in subsequent executions, the number for the first visit clears in the FIRST... THEN DO loop but continues to accrue for the subsequent visits. How do I get the program to accumulate these visits within each execution, but then clear them for the next time I run it? Thanks in advance ...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DATA bp_list;&lt;/P&gt;&lt;P&gt;SET bp_list END=last_obs;&lt;BR /&gt;BY subject;&lt;BR /&gt;RETAIN visit;&lt;BR /&gt;RETAIN subject_count 0;&lt;BR /&gt;&lt;BR /&gt;IF FIRST.subject THEN DO;&lt;BR /&gt;visit=0;&lt;BR /&gt;subject_count=subject_count+1;&lt;BR /&gt;END;&lt;BR /&gt;IF sbp NE . THEN visit=visit+1;&lt;BR /&gt;&lt;BR /&gt;IF _N_=1 THEN PUT @1 'ID'&lt;BR /&gt;@5 'DoB'&lt;BR /&gt;@15 'Age'&lt;BR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/105043"&gt;@23&lt;/a&gt;'Sex'&lt;BR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/75109"&gt;@30&lt;/a&gt;'Visit'&lt;BR /&gt;@40 'SBP'&lt;BR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/290987"&gt;@50&lt;/a&gt;'DBP' /;&lt;BR /&gt;&lt;BR /&gt;IF FIRST.subject THEN PUT @1 subject @5 dob MMDDYY8. @15 age 3.0 &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/54795"&gt;@25&lt;/a&gt; gender @;&lt;BR /&gt;IF sbp NE . THEN PUT &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/75109"&gt;@30&lt;/a&gt; visit @40 sbp &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/290987"&gt;@50&lt;/a&gt; dbp;&lt;BR /&gt;IF LAST.subject THEN PUT @3 'Subject ' subject ' has ' visit ' visits.' /;&lt;BR /&gt;IF last_obs THEN PUT @1 'The list includes ' subject_count ' subjects.';&lt;BR /&gt;RUN&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screen Shot 2020-02-27 at 6.24.24 AM.png" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/36433i70C7E851A72AFC95/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screen Shot 2020-02-27 at 6.24.24 AM.png" alt="Screen Shot 2020-02-27 at 6.24.24 AM.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Feb 2020 11:26:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Clearing-a-variable-counter-after-the-program-executes/m-p/627857#M185427</guid>
      <dc:creator>Newtrix</dc:creator>
      <dc:date>2020-02-27T11:26:48Z</dc:date>
    </item>
    <item>
      <title>Re: Clearing a variable counter after the program executes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Clearing-a-variable-counter-after-the-program-executes/m-p/627861#M185429</link>
      <description>&lt;P&gt;Please provide some input data, in the form of a DATA step, and another DATA step with the desired output data, and a description of what unexpected data values you are generating with your program.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I presume by "first time it's executed" you mean the first subject, yes? &amp;nbsp; Frankly it seems to me that your program represents the objective you describe, which is why I ask to see data.&lt;/P&gt;</description>
      <pubDate>Thu, 27 Feb 2020 11:30:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Clearing-a-variable-counter-after-the-program-executes/m-p/627861#M185429</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-02-27T11:30:28Z</dc:date>
    </item>
    <item>
      <title>Re: Clearing a variable counter after the program executes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Clearing-a-variable-counter-after-the-program-executes/m-p/627863#M185431</link>
      <description>&lt;P&gt;Thanks for responding. The issue isn't with the first time I run it - it works for that. But the next time I run it, the visit number clears for the first visit for each subject, but continues to accumulate for the second, third and fourth subjects. The screenshot at the bottom is from the second time it was run: first visit is fine, but the second visit in each case becomes the third, the third the fourth, and so on.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The data was merged from two files, several variables dropped, etc. - it's a lot to go thru if you don't need it. I can still provide if necessary but does the above clarify the issue?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Feb 2020 11:38:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Clearing-a-variable-counter-after-the-program-executes/m-p/627863#M185431</guid>
      <dc:creator>Newtrix</dc:creator>
      <dc:date>2020-02-27T11:38:08Z</dc:date>
    </item>
    <item>
      <title>Re: Clearing a variable counter after the program executes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Clearing-a-variable-counter-after-the-program-executes/m-p/627865#M185433</link>
      <description>&lt;P&gt;The problem could be caused by the first two lines of your program:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data bp_list;
  set bp_list end=last_obs;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Overwriting a dataset is rarely a good idea, if you have to execute a step multiple times in one session.&lt;/P&gt;</description>
      <pubDate>Thu, 27 Feb 2020 11:42:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Clearing-a-variable-counter-after-the-program-executes/m-p/627865#M185433</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-02-27T11:42:37Z</dc:date>
    </item>
    <item>
      <title>Re: Clearing a variable counter after the program executes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Clearing-a-variable-counter-after-the-program-executes/m-p/627867#M185434</link>
      <description>&lt;P&gt;This:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA bp_list;
SET bp_list END=last_obs;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;is where your problem comes from.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;NEVER&lt;/STRONG&gt; (unless you are a very experienced hand who knows what you're doing) overwrite the input dataset in the same step.&lt;/P&gt;
&lt;P&gt;Do this instead:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA bp_list_new;
SET bp_list END=last_obs;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and see how the data is changed subtly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Feb 2020 11:55:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Clearing-a-variable-counter-after-the-program-executes/m-p/627867#M185434</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-02-27T11:55:53Z</dc:date>
    </item>
    <item>
      <title>Re: Clearing a variable counter after the program executes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Clearing-a-variable-counter-after-the-program-executes/m-p/627868#M185435</link>
      <description>&lt;P&gt;Oh yes, and you absolutely need to show us your original dataset (before you run this step). Post example data in a data&amp;nbsp; step with datalines (this is absolutely necessary!), use the "little running man" to post code.&lt;/P&gt;</description>
      <pubDate>Thu, 27 Feb 2020 12:04:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Clearing-a-variable-counter-after-the-program-executes/m-p/627868#M185435</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-02-27T12:04:09Z</dc:date>
    </item>
    <item>
      <title>Re: Clearing a variable counter after the program executes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Clearing-a-variable-counter-after-the-program-executes/m-p/627869#M185436</link>
      <description>&lt;P&gt;If visit is already present in bp_list, then you'll have some funny effects.&lt;/P&gt;</description>
      <pubDate>Thu, 27 Feb 2020 12:07:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Clearing-a-variable-counter-after-the-program-executes/m-p/627869#M185436</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-02-27T12:07:55Z</dc:date>
    </item>
    <item>
      <title>Re: Clearing a variable counter after the program executes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Clearing-a-variable-counter-after-the-program-executes/m-p/627934#M185461</link>
      <description>&lt;P&gt;I do appreciate your help. This is the answer, I think - that I need to rename the dataset each time. It does require additional tweaking, though. When I renamed the set and ran it twice, I came up with the same issue. Is it because in the SET statement, I'm using the same bp_list dataset with the uncleared "visit" values?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's the entire, original code:&lt;/P&gt;&lt;P&gt;LIBNAME today '/folders/myfolders';&lt;/P&gt;&lt;P class="p1"&gt;DATA demog;&lt;/P&gt;&lt;P class="p1"&gt;INFORMAT dob MMDDYY10.;&lt;/P&gt;&lt;P class="p1"&gt;FORMAT dob MMDDYY10.;&lt;/P&gt;&lt;P class="p1"&gt;INPUT subject gender $ dob;&lt;/P&gt;&lt;P class="p1"&gt;DATALINES;&lt;/P&gt;&lt;P class="p1"&gt;1 &lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &lt;/SPAN&gt;F&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;03/03/1941&lt;/P&gt;&lt;P class="p1"&gt;2 &lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &lt;/SPAN&gt;F&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;04/09/1945&lt;/P&gt;&lt;P class="p1"&gt;3 &lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &lt;/SPAN&gt;M&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;07/04/1939&lt;/P&gt;&lt;P class="p1"&gt;4 &lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &lt;/SPAN&gt;F&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;04/19/1955&lt;/P&gt;&lt;P class="p1"&gt;5 &lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &lt;/SPAN&gt;M&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;10/06/1942&lt;/P&gt;&lt;P class="p1"&gt;6 &lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &lt;/SPAN&gt;M&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;08/08/1938&lt;/P&gt;&lt;P class="p1"&gt;7 &lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &lt;/SPAN&gt;F&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;01/16/1942&lt;/P&gt;&lt;P class="p1"&gt;8 &lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &lt;/SPAN&gt;M&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;06/23/1938&lt;/P&gt;&lt;P class="p1"&gt;9 &lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &lt;/SPAN&gt;F&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;05/15/1950&lt;/P&gt;&lt;P class="p1"&gt;10&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &lt;/SPAN&gt;F&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;11/06/1934&lt;/P&gt;&lt;P class="p1"&gt;11&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &lt;/SPAN&gt;M&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;10/04/1938&lt;/P&gt;&lt;P class="p1"&gt;12&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &lt;/SPAN&gt;M&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;02/16/1955&lt;/P&gt;&lt;P class="p1"&gt;13&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &lt;/SPAN&gt;M&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;02/15/1947&lt;/P&gt;&lt;P class="p1"&gt;14&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &lt;/SPAN&gt;F&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;04/11/1937&lt;/P&gt;&lt;P class="p1"&gt;15&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &lt;/SPAN&gt;F&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;04/07/1948&lt;/P&gt;&lt;P class="p1"&gt;;&lt;/P&gt;&lt;P class="p2"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;DATA bp;&lt;/P&gt;&lt;P class="p1"&gt;INFORMAT visit_date MMDDYY10.;&lt;/P&gt;&lt;P class="p1"&gt;FORMAT visit_date MMDDYY10.;&lt;/P&gt;&lt;P class="p2"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;RETAIN subject;&lt;/P&gt;&lt;P class="p1"&gt;INPUT temp 1 @;&lt;/P&gt;&lt;P class="p1"&gt;IF temp NE .THEN INPUT @1 subject @;&lt;/P&gt;&lt;P class="p1"&gt;INPUT @5 visit_date &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/162406"&gt;@20&lt;/a&gt; sbp &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/75109"&gt;@30&lt;/a&gt; dbp;&lt;/P&gt;&lt;P class="p1"&gt;DROP temp;&lt;/P&gt;&lt;P class="p2"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;DATALINES;&lt;/P&gt;&lt;P class="p1"&gt;1 &lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &lt;/SPAN&gt;08/13/2002&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;134 &lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;105&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;08/19/2002&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;136 &lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;112&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;08/29/2002&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;134 &lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;114&lt;/P&gt;&lt;P class="p1"&gt;2 &lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &lt;/SPAN&gt;09/20/2002&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;103&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;91&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;09/28/2002&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;105&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;82&lt;/P&gt;&lt;P class="p1"&gt;4 &lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &lt;/SPAN&gt;10/07/2002&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;146 &lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;113&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;10/17/2002&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;142 &lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;118&lt;/P&gt;&lt;P class="p1"&gt;5 &lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &lt;/SPAN&gt;08/31/2002&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;104&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;73&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;09/10/2002&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;111&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;77&lt;/P&gt;&lt;P class="p1"&gt;3 &lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &lt;/SPAN&gt;07/01/2002&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;148 &lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;106&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;07/05/2002&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;146 &lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;107&lt;/P&gt;&lt;P class="p1"&gt;;&lt;/P&gt;&lt;P class="p1"&gt;RUN;&lt;/P&gt;&lt;P class="p2"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;PROC SORT DATA=demog;&lt;/P&gt;&lt;P class="p1"&gt;BY subject;&lt;/P&gt;&lt;P class="p1"&gt;RUN;&lt;/P&gt;&lt;P class="p2"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;PROC SORT DATA=bp;&lt;/P&gt;&lt;P class="p1"&gt;BY subject;&lt;/P&gt;&lt;P class="p1"&gt;RUN;&lt;/P&gt;&lt;P class="p2"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;DATA all;&lt;/P&gt;&lt;P class="p1"&gt;MERGE demog bp;&lt;/P&gt;&lt;P class="p1"&gt;BY subject;&lt;/P&gt;&lt;P class="p1"&gt;RUN;&lt;/P&gt;&lt;P class="p2"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;DATA today.bp;&lt;/P&gt;&lt;P class="p1"&gt;SET all (RENAME=(visit_date=dov));&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;FORMAT dob MMDDYY10. age 4.1;&lt;/P&gt;&lt;P class="p1"&gt;IF sbp NE .;&lt;/P&gt;&lt;P class="p1"&gt;RUN;&lt;/P&gt;&lt;P class="p2"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;DATA bp_list;&lt;/P&gt;&lt;P class="p1"&gt;SET today.bp;&lt;/P&gt;&lt;P class="p1"&gt;age=(dov-dob)/365.25;&lt;/P&gt;&lt;P class="p1"&gt;PUT @1 subject @5 dob &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/162406"&gt;@20&lt;/a&gt; age 4.1 &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/75109"&gt;@30&lt;/a&gt; gender @35 sbp &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/227252"&gt;@45&lt;/a&gt; dbp;&lt;/P&gt;&lt;P class="p1"&gt;RUN;&lt;/P&gt;&lt;P class="p2"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;PROC SORT DATA=today.bp OUT=bp_list;&lt;/P&gt;&lt;P class="p1"&gt;BY subject dov;&lt;/P&gt;&lt;P class="p1"&gt;RUN;&lt;/P&gt;&lt;P class="p2"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;DATA bp_list;&lt;/P&gt;&lt;P class="p1"&gt;SET bp_list END=last_obs;&lt;/P&gt;&lt;P class="p2"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;BY subject;&lt;/P&gt;&lt;P class="p2"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;RETAIN visit;&lt;/P&gt;&lt;P class="p1"&gt;RETAIN subject_count 0;&lt;/P&gt;&lt;P class="p2"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;age=(dov-dob)/365.25;&lt;/P&gt;&lt;P class="p2"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;IF FIRST.subjectTHEN DO;&lt;/P&gt;&lt;P class="p1"&gt;visit=0;&lt;/P&gt;&lt;P class="p1"&gt;subject_count=subject_count+1;&lt;/P&gt;&lt;P class="p1"&gt;END;&lt;/P&gt;&lt;P class="p2"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;IF sbp NE .THEN visit=visit+1;&lt;/P&gt;&lt;P class="p2"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;IF _N_=1THEN PUT@1'ID'&lt;/P&gt;&lt;P class="p1"&gt;@5'DoB'&lt;/P&gt;&lt;P class="p1"&gt;@15 'Age'&lt;/P&gt;&lt;P class="p1"&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/105043"&gt;@23&lt;/a&gt;'Sex'&lt;/P&gt;&lt;P class="p1"&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/75109"&gt;@30&lt;/a&gt;'Visit'&lt;/P&gt;&lt;P class="p1"&gt;@40'SBP'&lt;/P&gt;&lt;P class="p1"&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/290987"&gt;@50&lt;/a&gt; 'DBP' /;&lt;/P&gt;&lt;P class="p2"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;IF FIRST.subjectTHEN PUT @1 subject @5 dob MMDDYY8. @15 age 3.0 &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/54795"&gt;@25&lt;/a&gt; gender @;&lt;/P&gt;&lt;P class="p2"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;IF sbp NE .THEN PUT &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/75109"&gt;@30&lt;/a&gt; visit @40 sbp &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/290987"&gt;@50&lt;/a&gt; dbp;&lt;/P&gt;&lt;P class="p2"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;IF LAST.subjectTHEN PUT @3 'Subject ' subject ' has ' visit ' visits.' /;&lt;/P&gt;&lt;P class="p2"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;IF last_obsTHEN PUT @1 'The list includes ' subject_count ' subjects.';&lt;/P&gt;&lt;P class="p1"&gt;RUN;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Feb 2020 15:08:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Clearing-a-variable-counter-after-the-program-executes/m-p/627934#M185461</guid>
      <dc:creator>Newtrix</dc:creator>
      <dc:date>2020-02-27T15:08:19Z</dc:date>
    </item>
    <item>
      <title>Re: Clearing a variable counter after the program executes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Clearing-a-variable-counter-after-the-program-executes/m-p/627953#M185468</link>
      <description>&lt;P&gt;Taking your code here what you're potentially looking for.&lt;/P&gt;
&lt;P&gt;Below should at least give you some ideas like how to use the&amp;nbsp;&lt;EM&gt;yrdif()&lt;/EM&gt; function to calculate the age (also does it right for leap years).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA demog;
  INFORMAT dob MMDDYY10.;
  FORMAT dob MMDDYY10.;
  INPUT subject gender $ dob;
  DATALINES;
1   F    03/03/1941
2   F    04/09/1945
3   M    07/04/1939
4   F    04/19/1955
5   M    10/06/1942
6   M    08/08/1938
7   F    01/16/1942
8   M    06/23/1938
9   F    05/15/1950
10  F    11/06/1934
11  M    10/04/1938
12  M    02/16/1955
13  M    02/15/1947
14  F    04/11/1937
15  F    04/07/1948
;

DATA bp;
  INFORMAT visit_date MMDDYY10.;
  FORMAT visit_date MMDDYY10.;
  RETAIN subject;
  INPUT temp 1 @;

  IF temp NE . THEN INPUT @1 subject @;
  INPUT @5 visit_date @20 sbp @30 dbp;
  DROP temp;
  DATALINES;
1   08/13/2002          134       105
    08/19/2002          136       112
    08/29/2002          134       114
2   09/20/2002          103        91
    09/28/2002          105        82
4   10/07/2002          146       113
    10/17/2002          142       118
5   08/31/2002          104        73
    09/10/2002          111        77
3   07/01/2002          148       106
    07/05/2002          146       107
;

PROC SORT DATA=demog;
  BY subject;
RUN;

PROC SORT DATA=bp;
  BY subject visit_date;
RUN;

DATA _null_;
  file print;
  MERGE demog bp(in=inbp rename=(visit_date=dov)) end=last_obs;
  BY subject;
  if inbp;

  FORMAT dob MMDDYY10. age 4.1;
  age=yrdif(dob, dov, 'age');
  IF FIRST.subject THEN
    DO;
      visit=0;
      subject_count+1;
    END;
  visit+1;

  IF _N_=1 THEN PUT @1 'ID'  @5 'DoB'  @15 'Age'  @23 'Sex'  @ /;
  IF FIRST.subject THEN PUT @1 subject @5 dob MMDDYY8. @15 age 3.0 @25 gender @;
  IF LAST.subject THEN PUT @30 'Subject ' subject ' has ' visit ' visits.' /;
  IF last_obs THEN PUT @1 'The list includes ' subject_count ' subjects.';
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Feb 2020 16:00:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Clearing-a-variable-counter-after-the-program-executes/m-p/627953#M185468</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2020-02-27T16:00:59Z</dc:date>
    </item>
    <item>
      <title>Re: Clearing a variable counter after the program executes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Clearing-a-variable-counter-after-the-program-executes/m-p/628077#M185525</link>
      <description>&lt;P&gt;Thanks! What caused my code not to clear? Also, I'm not getting the last line of code executed in my output file. I appreciate your help.&lt;/P&gt;</description>
      <pubDate>Thu, 27 Feb 2020 23:59:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Clearing-a-variable-counter-after-the-program-executes/m-p/628077#M185525</guid>
      <dc:creator>Newtrix</dc:creator>
      <dc:date>2020-02-27T23:59:04Z</dc:date>
    </item>
    <item>
      <title>Re: Clearing a variable counter after the program executes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Clearing-a-variable-counter-after-the-program-executes/m-p/628082#M185528</link>
      <description>&lt;P&gt;The RETAIN statement just says that you don't want the data step to reset the variable to missing when it starts the next iteration of the data step.&amp;nbsp; You do not need to use a RETAIN statement for a variable that is being sourced from an input dataset.&amp;nbsp; All such variables are automatically retained.&amp;nbsp; But in general you never noticed because the retained value is immediately overwritten when the next observation is read from the source dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What&amp;nbsp; others have tried to tell you is that by using the output of the first run as the input for the second run you are no longer going to get the same behavior for your "retained" variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The solution might be as simple as dropping the variable.&amp;nbsp; There is even an option you can change, DKRICOND , that you can change so that you could use the DROP= dataset option on your input dataset even if you are not sure if the variable you want to drop is there or not. Then you could run the same code against the same dataset name and not have it fail when it tries to drop the variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is a simple example that shows the issue.&amp;nbsp; Notice how in STEP3 the values of COUNTER are always one more than the value in STEP2 (except for the first observation per BY group).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But that the result in STEP4&amp;nbsp; when you don't allow the COUNTER variable to come in from STEP2 the counter is created the same it did when STEP2 was made.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=sashelp.class out=step1 ;
  by sex;
run;

data step2;
  set step1 ;
  by sex ;
  if first.sex then counter=0;
  counter+1;
run;

data step3;
 set step2;
 by sex;
 if first.sex then counter=0;
 counter+1;
run;

proc compare data=step2 compare=step3;
run;

data step4;
  set step2(drop=counter);
  by sex;
 if first.sex then counter=0;
 counter+1;
run;

proc compare data=step2 compare=step4;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 28 Feb 2020 00:36:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Clearing-a-variable-counter-after-the-program-executes/m-p/628082#M185528</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-02-28T00:36:20Z</dc:date>
    </item>
  </channel>
</rss>

