<?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: How do i aggregate and count a variable where X &amp;lt; 2 correctly? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-i-aggregate-and-count-a-variable-where-X-lt-2-correctly/m-p/956743#M373532</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/376676"&gt;@StickyRoll&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;How do i aggregate and count a variable where X &amp;lt; 2 using SAS Datastep?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, can i achieve the same thing using SAS Datastep?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Probably you could do this in a DATA step, but it is a lot easier using PROC FREQ to do counting or PROC SUMMARY to compute the N in each group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=driver nway;
    where mad_drvexpyr1&amp;lt;2;
    class mad_id mad_cls mad_year;
    var mad_age;
    output out=counts(drop=_type_ _freq_) n=mad_drvexpyr1_cnt;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 21 Jan 2025 14:15:30 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2025-01-21T14:15:30Z</dc:date>
    <item>
      <title>How do i aggregate and count a variable where X &lt; 2 correctly?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-i-aggregate-and-count-a-variable-where-X-lt-2-correctly/m-p/956726#M373525</link>
      <description>&lt;P&gt;How do i aggregate and count a variable where X &amp;lt; 2 using SAS Datastep?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have the following data:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data driver;
	MAD_ID='AAA';
	MAD_CLS='BLUE';
	MAD_YEAR='2024';
	MAD_AGE = 26;
	MAD_EXPYR = 7;
	MAD_DRVEXPYR1=1;
	output;

	MAD_ID='AAA';
	MAD_CLS='BLUE';
	MAD_YEAR='2024';
	MAD_AGE = 21;
	MAD_EXPYR = 2;
	MAD_DRVEXPYR1=1;
	output;

	MAD_ID='AAA';
	MAD_CLS='BLUE';
	MAD_YEAR='2024';
	MAD_AGE = 56;
	MAD_EXPYR = 15;
	MAD_DRVEXPYR1=5;
	output;

	MAD_ID='BBB';
	MAD_CLS='GREY';
	MAD_YEAR='2025';
	MAD_AGE = 18;
	MAD_EXPYR = 1;
	MAD_DRVEXPYR1=1;
	output;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, the result I am getting with the following SQL command is not what I want to achieve.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;proc sql;
	create table want as
	select *
	, count(1) as MAD_DRVEXPYR1_cnt
	from driver
	group by MAD_ID, MAD_CLS, MAD_YEAR
	having MAD_DRVEXPYR1 &amp;lt;2;

quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It shows&amp;nbsp;MAD_DRVEXPYR1 count as 3 for the first ID + CLS + YEAR but as we can see in the sample data, there is only 2 row of record for the first ID + CLS + YEAR.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Which part of my logic is wrong?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, can i achieve the same thing using SAS Datastep?&lt;/P&gt;</description>
      <pubDate>Tue, 21 Jan 2025 06:40:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-i-aggregate-and-count-a-variable-where-X-lt-2-correctly/m-p/956726#M373525</guid>
      <dc:creator>StickyRoll</dc:creator>
      <dc:date>2025-01-21T06:40:14Z</dc:date>
    </item>
    <item>
      <title>Re: How do i aggregate and count a variable where X &lt; 2 correctly?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-i-aggregate-and-count-a-variable-where-X-lt-2-correctly/m-p/956728#M373526</link>
      <description>&lt;P&gt;Use where instead of having. Having is used on the aggregated data.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	create table want as
	select *
	, count(*) as MAD_DRVEXPYR1_cnt
	from driver
    	where MAD_DRVEXPYR1 &amp;lt;2
	group by MAD_ID, MAD_CLS, MAD_YEAR
;

quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 21 Jan 2025 07:59:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-i-aggregate-and-count-a-variable-where-X-lt-2-correctly/m-p/956728#M373526</guid>
      <dc:creator>rudfaden</dc:creator>
      <dc:date>2025-01-21T07:59:32Z</dc:date>
    </item>
    <item>
      <title>Re: How do i aggregate and count a variable where X &lt; 2 correctly?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-i-aggregate-and-count-a-variable-where-X-lt-2-correctly/m-p/956743#M373532</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/376676"&gt;@StickyRoll&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;How do i aggregate and count a variable where X &amp;lt; 2 using SAS Datastep?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, can i achieve the same thing using SAS Datastep?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Probably you could do this in a DATA step, but it is a lot easier using PROC FREQ to do counting or PROC SUMMARY to compute the N in each group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=driver nway;
    where mad_drvexpyr1&amp;lt;2;
    class mad_id mad_cls mad_year;
    var mad_age;
    output out=counts(drop=_type_ _freq_) n=mad_drvexpyr1_cnt;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Jan 2025 14:15:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-i-aggregate-and-count-a-variable-where-X-lt-2-correctly/m-p/956743#M373532</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-01-21T14:15:30Z</dc:date>
    </item>
    <item>
      <title>Re: How do i aggregate and count a variable where X &lt; 2 correctly?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-i-aggregate-and-count-a-variable-where-X-lt-2-correctly/m-p/956823#M373558</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp; Thanks for the Proc Freq suggestion. It works. However, may i find out on the "var" part? mad_age is from the source but is not used for the computation. The variable that I am looking at is actually "MAD_DRVEXPYR1" instead. Do i just substitute it with "MAD_DRVEXPYR1" for VAR?&lt;/P&gt;</description>
      <pubDate>Wed, 22 Jan 2025 01:50:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-i-aggregate-and-count-a-variable-where-X-lt-2-correctly/m-p/956823#M373558</guid>
      <dc:creator>StickyRoll</dc:creator>
      <dc:date>2025-01-22T01:50:37Z</dc:date>
    </item>
    <item>
      <title>Re: How do i aggregate and count a variable where X &lt; 2 correctly?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-i-aggregate-and-count-a-variable-where-X-lt-2-correctly/m-p/956825#M373560</link>
      <description>&lt;P&gt;Yes.&amp;nbsp; But what is it you want to count?&amp;nbsp; Your original SQL code was counting the number of observations, whether they were missing or not.&amp;nbsp; Is that what you want? If so the _FREQ_ automatic variable will have that count.&amp;nbsp; The N statistic of PROC SUMMARY (also known as PROC MEANS) will count the number of non missing values, like using count(varname) in SQL code will do.&amp;nbsp; If you don't have any missing values then the automatic variable _FREQ_ will be the same as the N statistic.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But that variable seems to already be a type of count, if I am guessing the meaning properly.&amp;nbsp; Do you instead want to take the SUM of the years of experience?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or perhaps use that variable as another CLASS variable to get counts by different levels of experience? Perhaps using a format bin the years into a small number of categories, like low and high.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Jan 2025 02:07:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-i-aggregate-and-count-a-variable-where-X-lt-2-correctly/m-p/956825#M373560</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-01-22T02:07:19Z</dc:date>
    </item>
    <item>
      <title>Re: How do i aggregate and count a variable where X &lt; 2 correctly?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-i-aggregate-and-count-a-variable-where-X-lt-2-correctly/m-p/956826#M373561</link>
      <description>&lt;P&gt;You don't have to work so hard to make sample data.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;After all you are using SAS now.&amp;nbsp; Take advantage of that fact.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data driver;
  input MAD_ID :$3. MAD_CLS :$4. MAD_YEAR :$4. MAD_AGE MAD_EXPYR MADDRVEXPYR1 ;
cards;
AAA BLUE 2024 26  7 1
AAA BLUE 2024 21  2 1
AAA BLUE 2024 56 15 5
BBB GREY 2025 18  1 1
;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 Jan 2025 02:09:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-i-aggregate-and-count-a-variable-where-X-lt-2-correctly/m-p/956826#M373561</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-01-22T02:09:17Z</dc:date>
    </item>
    <item>
      <title>Re: How do i aggregate and count a variable where X &lt; 2 correctly?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-i-aggregate-and-count-a-variable-where-X-lt-2-correctly/m-p/956865#M373577</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/376676"&gt;@StickyRoll&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp; Thanks for the Proc Freq suggestion. It works. However, may i find out on the "var" part? mad_age is from the source but is not used for the computation. The variable that I am looking at is actually "MAD_DRVEXPYR1" instead. Do i just substitute it with "MAD_DRVEXPYR1" for VAR?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I am just counting rows, so it doesn't matter which numeric variable I choose to do the counting of rows. MAD_AGE would give the same answer for this data set as any other numeric variable. (Assuming there are no missing values)&lt;/P&gt;</description>
      <pubDate>Wed, 22 Jan 2025 11:40:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-i-aggregate-and-count-a-variable-where-X-lt-2-correctly/m-p/956865#M373577</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-01-22T11:40:52Z</dc:date>
    </item>
  </channel>
</rss>

