<?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: Keeping counting variable in the SAS table. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Keeping-counting-variable-in-the-SAS-table/m-p/8925#M412</link>
    <description>Hi:&lt;BR /&gt;
  I'm not sure why you have mixed macro variables into this need to get a COUNT variable in a table.&lt;BR /&gt;
                                                             &lt;BR /&gt;
If you use BY group processing, SAS creates some automatic variables that signal when your program is reading the first of a BY variable group or the last of a BY variable group. This happens automatically in a DATA step program. You might investigate FIRST.byvar and/or LAST.byvar processing.&lt;BR /&gt;
 &lt;BR /&gt;
For example, consider this data:&lt;BR /&gt;
[pre]&lt;BR /&gt;
12 alan 100 member&lt;BR /&gt;
12 anne 200 spouse&lt;BR /&gt;
14 bob  111 member &lt;BR /&gt;
23 carla 112 member&lt;BR /&gt;
24 dana  113 member&lt;BR /&gt;
24 dave  114 spouse&lt;BR /&gt;
35 edna  115 member&lt;BR /&gt;
[/pre]&lt;BR /&gt;
                                                       &lt;BR /&gt;
When a SAS program encounters this data, if you turn BY processing ON for the ID variable, then just looking at ID and name, the values of FIRST.ID and LAST.ID for every observation would be:&lt;BR /&gt;
[pre]&lt;BR /&gt;
ID NAME FIRST.ID LAST.ID&lt;BR /&gt;
12 alan   1        0&lt;BR /&gt;
12 anne   0        1&lt;BR /&gt;
14 bob    1        1&lt;BR /&gt;
23 carla  1        1&lt;BR /&gt;
24 dana   1        0&lt;BR /&gt;
24 dave   0        1&lt;BR /&gt;
35 edna   1        1&lt;BR /&gt;
[/pre]&lt;BR /&gt;
 &lt;BR /&gt;
The FIRST. and LAST. variables are not written to your final dataset, they are just available for processing and testing. This allows you to do certain things (like increment a COUNT variable, for a certain condition, such as:&lt;BR /&gt;
[pre]&lt;BR /&gt;
if first.id = 1 then count = count + 1;&lt;BR /&gt;
OR&lt;BR /&gt;
if first.id then count+1;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
 &lt;BR /&gt;
Those last 2 statements are equivalent statements. When a variable has a value of 0 or 1, you can consider 0= false or 1=true. So the "boolean" logic way to do a test is to write a shortcut condition, such as "if first.id". The only other statement that you need to read about is that RETAIN statement, because normally, variable values are "reset" or reinitialized between each execution of the Data step program. Using the RETAIN statement instructs SAS that there is a variable you want to have it "remember" and not reset between each execution of the DATA step program.&lt;BR /&gt;
 &lt;BR /&gt;
You seem to want to hold the ID value in the TEMP variable so you can test the previous variable value with the current variable value. This could be achieved using the LAG function, but for a simple count, it's not necessary.&lt;BR /&gt;
 &lt;BR /&gt;
Here's a program that reads some fake data and then creates a count variable in a table called "newmember":&lt;BR /&gt;
[pre]&lt;BR /&gt;
data info;&lt;BR /&gt;
  infile datalines;&lt;BR /&gt;
  input ID name $ amt typejoin $;&lt;BR /&gt;
return;&lt;BR /&gt;
datalines;&lt;BR /&gt;
12 alan 100 member&lt;BR /&gt;
12 anne 200 spouse&lt;BR /&gt;
14 bob  111 member &lt;BR /&gt;
23 carla 112 member&lt;BR /&gt;
24 dana  113 member&lt;BR /&gt;
24 dave  114 spouse&lt;BR /&gt;
35 edna  115 member&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
                   &lt;BR /&gt;
proc sort data=info;&lt;BR /&gt;
  by id typejoin;&lt;BR /&gt;
run;&lt;BR /&gt;
            &lt;BR /&gt;
data newmember;&lt;BR /&gt;
  set info;&lt;BR /&gt;
  by ID;&lt;BR /&gt;
  retain count 0;&lt;BR /&gt;
  if first.ID then count+1;&lt;BR /&gt;
run;&lt;BR /&gt;
                    &lt;BR /&gt;
options nodate nonumber nocenter;&lt;BR /&gt;
         &lt;BR /&gt;
proc print data=newmember;&lt;BR /&gt;
  var id count name typejoin amt;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
And the output is shown below.&lt;BR /&gt;
      &lt;BR /&gt;
cynthia&lt;BR /&gt;
[pre]&lt;BR /&gt;
Obs    ID    count    name     typejoin    amt&lt;BR /&gt;
                                          &lt;BR /&gt;
 1     12      1      alan      member     100&lt;BR /&gt;
 2     12      1      anne      spouse     200&lt;BR /&gt;
 3     14      2      bob       member     111&lt;BR /&gt;
 4     23      3      carla     member     112&lt;BR /&gt;
 5     24      4      dana      member     113&lt;BR /&gt;
 6     24      4      dave      spouse     114&lt;BR /&gt;
 7     35      5      edna      member     115&lt;BR /&gt;
                 &lt;BR /&gt;
[/pre]</description>
    <pubDate>Sun, 22 Mar 2009 01:48:17 GMT</pubDate>
    <dc:creator>Cynthia_sas</dc:creator>
    <dc:date>2009-03-22T01:48:17Z</dc:date>
    <item>
      <title>Keeping counting variable in the SAS table.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keeping-counting-variable-in-the-SAS-table/m-p/8924#M411</link>
      <description>I have a table with the numeric variable ID among several others. The table is sorted by ID.&lt;BR /&gt;
I want to add a new variable, COUNT, that is incremented for each new ID.&lt;BR /&gt;
&lt;BR /&gt;
Like this:&lt;BR /&gt;
&lt;BR /&gt;
ID COUNT&lt;BR /&gt;
12      1&lt;BR /&gt;
12      1&lt;BR /&gt;
14      2      &lt;BR /&gt;
23      3&lt;BR /&gt;
24      4&lt;BR /&gt;
24      4&lt;BR /&gt;
35      5&lt;BR /&gt;
&lt;BR /&gt;
For the first data step I could compare ID to an impossible ID value of - 1.&lt;BR /&gt;
&lt;BR /&gt;
I declare %let TEMp=-1, and compare in each data step.&lt;BR /&gt;
&lt;BR /&gt;
if ID=&amp;amp;temp, COUNT should not be incremented.&lt;BR /&gt;
&lt;BR /&gt;
if ID~=TEMP, then I increment COUNT and assign the new ID to TEMP.&lt;BR /&gt;
&lt;BR /&gt;
But it didn't work the expected way.&lt;BR /&gt;
&lt;BR /&gt;
My table looked like this:&lt;BR /&gt;
&lt;BR /&gt;
ID COUNT&lt;BR /&gt;
12      1&lt;BR /&gt;
12      2&lt;BR /&gt;
14      3      &lt;BR /&gt;
23      4&lt;BR /&gt;
24      5&lt;BR /&gt;
24      6&lt;BR /&gt;
35      7&lt;BR /&gt;
&lt;BR /&gt;
I am not sure how to assign TEMP when there was a new ID.&lt;BR /&gt;
&lt;BR /&gt;
&amp;amp;TEMP=ID&lt;BR /&gt;
&lt;BR /&gt;
or&lt;BR /&gt;
&lt;BR /&gt;
TEMP=ID.&lt;BR /&gt;
&lt;BR /&gt;
For the first step the if expression must be: If ID~=&amp;amp;TEMP , with the ampersand.&lt;BR /&gt;
&lt;BR /&gt;
Conflict between numeric and character variable or what have I done wrong?</description>
      <pubDate>Sat, 21 Mar 2009 21:28:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keeping-counting-variable-in-the-SAS-table/m-p/8924#M411</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-03-21T21:28:15Z</dc:date>
    </item>
    <item>
      <title>Re: Keeping counting variable in the SAS table.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keeping-counting-variable-in-the-SAS-table/m-p/8925#M412</link>
      <description>Hi:&lt;BR /&gt;
  I'm not sure why you have mixed macro variables into this need to get a COUNT variable in a table.&lt;BR /&gt;
                                                             &lt;BR /&gt;
If you use BY group processing, SAS creates some automatic variables that signal when your program is reading the first of a BY variable group or the last of a BY variable group. This happens automatically in a DATA step program. You might investigate FIRST.byvar and/or LAST.byvar processing.&lt;BR /&gt;
 &lt;BR /&gt;
For example, consider this data:&lt;BR /&gt;
[pre]&lt;BR /&gt;
12 alan 100 member&lt;BR /&gt;
12 anne 200 spouse&lt;BR /&gt;
14 bob  111 member &lt;BR /&gt;
23 carla 112 member&lt;BR /&gt;
24 dana  113 member&lt;BR /&gt;
24 dave  114 spouse&lt;BR /&gt;
35 edna  115 member&lt;BR /&gt;
[/pre]&lt;BR /&gt;
                                                       &lt;BR /&gt;
When a SAS program encounters this data, if you turn BY processing ON for the ID variable, then just looking at ID and name, the values of FIRST.ID and LAST.ID for every observation would be:&lt;BR /&gt;
[pre]&lt;BR /&gt;
ID NAME FIRST.ID LAST.ID&lt;BR /&gt;
12 alan   1        0&lt;BR /&gt;
12 anne   0        1&lt;BR /&gt;
14 bob    1        1&lt;BR /&gt;
23 carla  1        1&lt;BR /&gt;
24 dana   1        0&lt;BR /&gt;
24 dave   0        1&lt;BR /&gt;
35 edna   1        1&lt;BR /&gt;
[/pre]&lt;BR /&gt;
 &lt;BR /&gt;
The FIRST. and LAST. variables are not written to your final dataset, they are just available for processing and testing. This allows you to do certain things (like increment a COUNT variable, for a certain condition, such as:&lt;BR /&gt;
[pre]&lt;BR /&gt;
if first.id = 1 then count = count + 1;&lt;BR /&gt;
OR&lt;BR /&gt;
if first.id then count+1;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
 &lt;BR /&gt;
Those last 2 statements are equivalent statements. When a variable has a value of 0 or 1, you can consider 0= false or 1=true. So the "boolean" logic way to do a test is to write a shortcut condition, such as "if first.id". The only other statement that you need to read about is that RETAIN statement, because normally, variable values are "reset" or reinitialized between each execution of the Data step program. Using the RETAIN statement instructs SAS that there is a variable you want to have it "remember" and not reset between each execution of the DATA step program.&lt;BR /&gt;
 &lt;BR /&gt;
You seem to want to hold the ID value in the TEMP variable so you can test the previous variable value with the current variable value. This could be achieved using the LAG function, but for a simple count, it's not necessary.&lt;BR /&gt;
 &lt;BR /&gt;
Here's a program that reads some fake data and then creates a count variable in a table called "newmember":&lt;BR /&gt;
[pre]&lt;BR /&gt;
data info;&lt;BR /&gt;
  infile datalines;&lt;BR /&gt;
  input ID name $ amt typejoin $;&lt;BR /&gt;
return;&lt;BR /&gt;
datalines;&lt;BR /&gt;
12 alan 100 member&lt;BR /&gt;
12 anne 200 spouse&lt;BR /&gt;
14 bob  111 member &lt;BR /&gt;
23 carla 112 member&lt;BR /&gt;
24 dana  113 member&lt;BR /&gt;
24 dave  114 spouse&lt;BR /&gt;
35 edna  115 member&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
                   &lt;BR /&gt;
proc sort data=info;&lt;BR /&gt;
  by id typejoin;&lt;BR /&gt;
run;&lt;BR /&gt;
            &lt;BR /&gt;
data newmember;&lt;BR /&gt;
  set info;&lt;BR /&gt;
  by ID;&lt;BR /&gt;
  retain count 0;&lt;BR /&gt;
  if first.ID then count+1;&lt;BR /&gt;
run;&lt;BR /&gt;
                    &lt;BR /&gt;
options nodate nonumber nocenter;&lt;BR /&gt;
         &lt;BR /&gt;
proc print data=newmember;&lt;BR /&gt;
  var id count name typejoin amt;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
And the output is shown below.&lt;BR /&gt;
      &lt;BR /&gt;
cynthia&lt;BR /&gt;
[pre]&lt;BR /&gt;
Obs    ID    count    name     typejoin    amt&lt;BR /&gt;
                                          &lt;BR /&gt;
 1     12      1      alan      member     100&lt;BR /&gt;
 2     12      1      anne      spouse     200&lt;BR /&gt;
 3     14      2      bob       member     111&lt;BR /&gt;
 4     23      3      carla     member     112&lt;BR /&gt;
 5     24      4      dana      member     113&lt;BR /&gt;
 6     24      4      dave      spouse     114&lt;BR /&gt;
 7     35      5      edna      member     115&lt;BR /&gt;
                 &lt;BR /&gt;
[/pre]</description>
      <pubDate>Sun, 22 Mar 2009 01:48:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keeping-counting-variable-in-the-SAS-table/m-p/8925#M412</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2009-03-22T01:48:17Z</dc:date>
    </item>
  </channel>
</rss>

