<?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: Creating new variable that counts number of visits in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Creating-new-variable-that-counts-across-each-observation/m-p/935871#M42059</link>
    <description>&lt;P&gt;You might want to change the code based on your logic, but it sounds like you want to check if the sum of the indicator values (sample_i ) for each observation are greater than 0. If it is, then assign a value of 1, otherwise 0.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set count;

	tests=0;
	if sum(of sample_:)&amp;gt;0 then tests=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 15 Jul 2024 21:32:45 GMT</pubDate>
    <dc:creator>antonbcristina</dc:creator>
    <dc:date>2024-07-15T21:32:45Z</dc:date>
    <item>
      <title>Creating new variable that counts across each observation</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-new-variable-that-counts-across-each-observation/m-p/935865#M42051</link>
      <description>&lt;P&gt;I would like to count the number of tests collected. For each observation, a sample collected would be counted as 1. For example if visit_id 01 had samples 1, 2, 3 and 4 collected that would be counted as one. If visit_id 06 has zero samples collected that would be 0. I'm guessing I have to create a new variable that does that, I'm just not sure how. Thank you.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;DATA COUNT;
INPUT VISIT_ID SAMPLE_1 SAMPLE_2 SAMPLE_3 SAMPLE_4;
CARDS;

01 1 1 0 1
02 1 0 1 1
03 0 0 1 0
04 1 1 1 1&lt;BR /&gt;05 1 0 0 0&lt;BR /&gt;06 0 0 0 0
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 15 Jul 2024 21:02:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-new-variable-that-counts-across-each-observation/m-p/935865#M42051</guid>
      <dc:creator>shami</dc:creator>
      <dc:date>2024-07-15T21:02:06Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variable that counts across each observation</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-new-variable-that-counts-across-each-observation/m-p/935866#M42052</link>
      <description>&lt;P&gt;Your words and subject line are unclear. You say "new variable that counts across" but later you say "For example if visit_id 01 had samples 1, 2, 3 and 4 collected that would be counted as one." These two are contradictory. I don't really know what you want&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Nevertheless, if you have 0s and 1s, then "counts across", if that is what you want, is done like this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
     set have;
     count=sum(of sample_1-sample_4);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 15 Jul 2024 21:06:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-new-variable-that-counts-across-each-observation/m-p/935866#M42052</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-07-15T21:06:40Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variable that counts across each observation</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-new-variable-that-counts-across-each-observation/m-p/935867#M42053</link>
      <description>&lt;P&gt;My apologies for the unclear subject line. The body describes exactly what I'm trying to do. I don't want to count across. I'm sorry for the confusion.&lt;/P&gt;</description>
      <pubDate>Mon, 15 Jul 2024 21:10:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-new-variable-that-counts-across-each-observation/m-p/935867#M42053</guid>
      <dc:creator>shami</dc:creator>
      <dc:date>2024-07-15T21:10:28Z</dc:date>
    </item>
    <item>
      <title>Creating new variable that counts number of visits</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-new-variable-that-counts-across-each-observation/m-p/935868#M42058</link>
      <description>&lt;P&gt;I would like to count the number of tests collected. For each observation, a sample collected would be counted as 1. For example if visit_id 01 had samples 1, 2, 3 and 4 collected that would be counted as one. If visit_id 06 has zero samples collected that would be 0. I'm guessing I have to create a new variable that does that, I'm just not sure how. Thank you.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;DATA COUNT;
INPUT VISIT_ID SAMPLE_1 SAMPLE_2 SAMPLE_3 SAMPLE_4;
CARDS;

01 1 1 0 1
02 1 0 1 1
03 0 0 1 0
04 1 1 1 1&lt;BR /&gt;05 1 0 0 0&lt;BR /&gt;06 0 0 0 0
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 15 Jul 2024 21:20:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-new-variable-that-counts-across-each-observation/m-p/935868#M42058</guid>
      <dc:creator>shami</dc:creator>
      <dc:date>2024-07-15T21:20:15Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variable that counts across each observation</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-new-variable-that-counts-across-each-observation/m-p/935870#M42055</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/437174"&gt;@shami&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I would like to count the number of tests collected. For each observation, a sample collected would be counted as 1. For example if visit_id 01 had samples 1, 2, 3 and 4 collected that would be counted as one. If visit_id 06 has zero samples collected that would be 0. I'm guessing I have to create a new variable that does that, I'm just not sure how. Thank you.&lt;CODE class=""&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;It really helps to provide a general rule stating variable names and values. Such as Count the observations where any of the variables Sample1 through Sample4 have at least one value &amp;gt; 0. Then show an example of the desired output based on your given example.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For my description above:&lt;/P&gt;
&lt;PRE&gt;proc means data=count n;
where max(SAMPLE_1, SAMPLE_2, SAMPLE_3, SAMPLE_4)&amp;gt; 0;
var visit_id;
run;&lt;/PRE&gt;
&lt;P&gt;Which shows an output like:&lt;/P&gt;
&lt;DIV class="branch"&gt;
&lt;DIV&gt;
&lt;DIV align="left"&gt;
&lt;TABLE class="table" summary="Procedure Means: Summary statistics" cellspacing="0" cellpadding="3"&gt;&lt;COLGROUP&gt; &lt;COL /&gt;&lt;/COLGROUP&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="c m header" scope="col"&gt;Analysis&lt;BR /&gt;Variable&lt;BR /&gt;: VISIT_ID&lt;/TH&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="c m header" scope="col"&gt;N&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;5&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;P&gt;The Where statement means that SAS will use any observation where there is at least one value greater than 0 for the procedure.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Caveat: this would include 0.0001 as well if by chance one of the variables had such.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Jul 2024 21:31:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-new-variable-that-counts-across-each-observation/m-p/935870#M42055</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-07-15T21:31:11Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variable that counts number of visits</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-new-variable-that-counts-across-each-observation/m-p/935871#M42059</link>
      <description>&lt;P&gt;You might want to change the code based on your logic, but it sounds like you want to check if the sum of the indicator values (sample_i ) for each observation are greater than 0. If it is, then assign a value of 1, otherwise 0.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set count;

	tests=0;
	if sum(of sample_:)&amp;gt;0 then tests=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 15 Jul 2024 21:32:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-new-variable-that-counts-across-each-observation/m-p/935871#M42059</guid>
      <dc:creator>antonbcristina</dc:creator>
      <dc:date>2024-07-15T21:32:45Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variable that counts across each observation</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-new-variable-that-counts-across-each-observation/m-p/935873#M42057</link>
      <description>&lt;P&gt;Your descriptions are confusing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to count the total number of SAMPLES then you want to sum both ways.&amp;nbsp; Order should not matter.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So assuming that the values of the SAMPLE_n variables represents the number of test done on each sample you could use the SAS function SUM(,) to add the values in each observation and then the SQL aggregate function SUM() to find the grand total across all observations.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  select sum( sum(sample_1,sample_2,sample_3_sample_4)) as grand_count
  from count
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But since the values you show are only 1 or 0 perhaps the SAMPLE_n variables indicate whether or not any samples were taken?&amp;nbsp; (If so then why was the dataset named COUNT??)&amp;nbsp; And perhaps what you want to find out is how many observations had ANY samples taken?&amp;nbsp; In that case for boolean 1/0 variables the MAX() will indicate if any of them was true or not.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In that case you might want to do something like this using the SAS function MAX(,) and then the SQL function SUM().&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql
select sum(max(sample_1,sample_2,sample_3,sample_4)) 
   as nvisits label='Number of visits with any samples'
from count;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 15 Jul 2024 23:52:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-new-variable-that-counts-across-each-observation/m-p/935873#M42057</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-07-15T23:52:01Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variable that counts number of visits</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-new-variable-that-counts-across-each-observation/m-p/935887#M42060</link>
      <description>&lt;P&gt;Please do not double-post.&lt;/P&gt;</description>
      <pubDate>Tue, 16 Jul 2024 07:42:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-new-variable-that-counts-across-each-observation/m-p/935887#M42060</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-07-16T07:42:29Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variable that counts across each observation</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-new-variable-that-counts-across-each-observation/m-p/935894#M42061</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/437174"&gt;@shami&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;My apologies for the unclear subject line. The body describes exactly what I'm trying to do. I don't want to count across. I'm sorry for the confusion.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So I showed how to compute this for the sum. You wanted 1 or 0. Got it. In my example, instead of sum, here's a thought question for you ... what statistic, instead of sum, gives you a 1 or 0 depending on whether there is a 1 in the row, or all zeros in the row?&lt;/P&gt;</description>
      <pubDate>Tue, 16 Jul 2024 13:27:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-new-variable-that-counts-across-each-observation/m-p/935894#M42061</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-07-16T13:27:50Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variable that counts number of visits</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-new-variable-that-counts-across-each-observation/m-p/935923#M42062</link>
      <description>Noted.</description>
      <pubDate>Tue, 16 Jul 2024 13:17:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-new-variable-that-counts-across-each-observation/m-p/935923#M42062</guid>
      <dc:creator>shami</dc:creator>
      <dc:date>2024-07-16T13:17:09Z</dc:date>
    </item>
  </channel>
</rss>

