<?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: sql question in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/sql-question/m-p/810396#M319578</link>
    <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/65907"&gt;@wlierman&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You have not specified what is the error you are having OR how you want the error to be.&lt;BR /&gt;From the SQL code you have given, &amp;nbsp;I see that you have included the aggregate column (Contact_ID) in the select statement and your group statement does not include the non-aggregate columns. The result that comes in this scenario would be confusing or not what one would expect. I am giving a specimen code without the above two factors and you can see the difference.&lt;BR /&gt;Experiment a bit and I am sure you would get what you want.&lt;/P&gt;
&lt;P&gt;Lastly, a very important fact, please provide the source data as a datastep or atleast properly formatted. I have taken only three rows, try using the complete data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
Retain Contact_ID NAICS_Sector Sector_Type Date N;
informat Date ANYDTDTE9.;
Format date date9.;
length Sector_Type $ 20;
input Contact_ID NAICS_Sector $ Sector_Type $ Date N;
datalines;
1056200 GOODS AGRICULTURE_FORESTRY 10-Jun-20 1
1069490 GOODS AGRICULTURE_FORESTRY 13-Jun-20 1
1012517 GOODS MANUFACTURING 13-Jun-20 1
;
run;
Proc sql noprint;
CREATE Table want AS
   SELECT Date, Count(Contact_ID) as Number_of_contacts,NAICS_Sector, N
   FROM have
   Where '01APR20'd &amp;lt;= Date &amp;lt;= '31JAN22'd
   And NAICS_Sector = "GOODS"
   Group by date ,NAICS_Sector,N 
   Order by 1;
   quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The output would be like this&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Sajid01_0-1651159806871.png" style="width: 556px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/70947iB6FEC4F254289B0E/image-dimensions/556x137?v=v2" width="556" height="137" role="button" title="Sajid01_0-1651159806871.png" alt="Sajid01_0-1651159806871.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 28 Apr 2022 15:33:02 GMT</pubDate>
    <dc:creator>Sajid01</dc:creator>
    <dc:date>2022-04-28T15:33:02Z</dc:date>
    <item>
      <title>sql question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sql-question/m-p/810276#M319530</link>
      <description>&lt;P&gt;I have a quick sql question that involves counting up several values by individual dates.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Contact_ID	     NAICS_Sector	     Sector_Type	      Date	    N
1056200	              GOODS	      AGRICULTURE FORESTRY	10-Jun-20	1
1069490		          GOODS	      AGRICULTURE FORESTRY	13-Jun-20	1
1012517		          GOODS	        MANUFACTURING	    13-Jun-20	1
1074445			      GOODS	        MANUFACTURING	    14-Jun-20	1
1072985		          GOODS	        MANUFACTURING	    14-Jun-20	1
951206			      GOODS	        MANUFACTURING	    14-Jun-20	1
1074884		          GOODS	        MANUFACTURING	    14-Jun-20	1
1074374			      GOODS	        MANUFACTURING	    14-Jun-20	1
1074312			      GOODS	        MANUFACTURING	    14-Jun-20	1
1077220			      GOODS	        MANUFACTURING	    15-Jun-20	1
1079736		          GOODS	        MANUFACTURING	    16-Jun-20	1
1083450		          GOODS	        CONSTRUCTION	    17-Jun-20	1
1087981			      GOODS	        MANUFACTURING	    18-Jun-20	1
1090459			      GOODS	      AGRICULTURE FORESTRY	18-Jun-20	1
1090413		          GOODS	      AGRICULTURE FORESTRY	18-Jun-20	1
9991088408			  GOODS	        MANUFACTURING	    18-Jun-20	1
1089620	              GOODS	        MANUFACTURING	    18-Jun-20	1
1092420	      		  GOODS	        MANUFACTURING	    19-Jun-20	1
9991088337			  GOODS	        MANUFACTURING	    19-Jun-20	1
1081349		          GOODS	        MANUFACTURING	    20-Jun-20	1
1098376			      GOODS	        MANUFACTURING	    20-Jun-20	1
1097263			      GOODS	        MANUFACTURING	    20-Jun-20	1
1098412		          GOODS	        CONSTRUCTION	    20-Jun-20	1
1080505		          GOODS	        MANUFACTURING	    20-Jun-20	1
1098407			      GOODS	        MANUFACTURING	    20-Jun-20	1
1098432			      GOODS	        CONSTRUCTION	    20-Jun-20	1
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The above rows show what the data basically looks like.&amp;nbsp; What I wanted is for all the same dates the total of the count. For example,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 19-Jun-20&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 20-Jun-20&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 6&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and so on.&amp;nbsp; My sql code looks like&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Proc sql noprint;
CREATE Table SAS_EMPL.POE_Goods_Trend AS
   SELECT Contact_Person_ID,
          County_1,
		  Race_Ethnicity,
		  NAICS_Sector,
		  Sector_Type,
		  datepart(Created_On)AS Date label = "Date" format=date9.,
		  count(distinct Contact_Person_ID) AS N
   FROM SAS_EMPL.POE_GOODS_FINI
   Where '01APR20'd &amp;lt;= Calculated Date &amp;lt;= '31JAN22'd
   And NAICS_Sector = "GOODS"
   Group by date
   Order by 1;
   quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But doesn't add up properly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I appreciate your assistance on this question.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;TRhank you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;wlierman&lt;/P&gt;</description>
      <pubDate>Wed, 27 Apr 2022 21:30:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sql-question/m-p/810276#M319530</guid>
      <dc:creator>wlierman</dc:creator>
      <dc:date>2022-04-27T21:30:04Z</dc:date>
    </item>
    <item>
      <title>Re: sql question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sql-question/m-p/810277#M319531</link>
      <description>Do you want that total added in as a new column to the table above or summarized?&lt;BR /&gt;Your query has a note about re-merging statistics I assume?</description>
      <pubDate>Wed, 27 Apr 2022 21:41:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sql-question/m-p/810277#M319531</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-04-27T21:41:52Z</dc:date>
    </item>
    <item>
      <title>Re: sql question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sql-question/m-p/810286#M319534</link>
      <description>&lt;P&gt;Yes I did want a separate column.&amp;nbsp; And my query did have the remerging note.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Apr 2022 22:46:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sql-question/m-p/810286#M319534</guid>
      <dc:creator>wlierman</dc:creator>
      <dc:date>2022-04-27T22:46:04Z</dc:date>
    </item>
    <item>
      <title>Re: sql question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sql-question/m-p/810289#M319536</link>
      <description>Yes I did want a total column.&amp;nbsp; The remerging note also appeared in the log.&lt;BR /&gt;Thank you.&lt;BR /&gt;wlierman&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 28 Apr 2022 00:02:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sql-question/m-p/810289#M319536</guid>
      <dc:creator>wlierman</dc:creator>
      <dc:date>2022-04-28T00:02:24Z</dc:date>
    </item>
    <item>
      <title>Re: sql question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sql-question/m-p/810298#M319542</link>
      <description>&lt;P&gt;Why do you select all columns, when you are only interested in the date and count?&lt;/P&gt;</description>
      <pubDate>Thu, 28 Apr 2022 04:32:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sql-question/m-p/810298#M319542</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-04-28T04:32:41Z</dc:date>
    </item>
    <item>
      <title>Re: sql question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sql-question/m-p/810352#M319567</link>
      <description>&lt;P&gt;I do some other analytics after the total count is calculated.&amp;nbsp; Also wanted to&lt;/P&gt;
&lt;P&gt;to give more detail of the xlsx spreadsheet.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;wlierman&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Apr 2022 13:32:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sql-question/m-p/810352#M319567</guid>
      <dc:creator>wlierman</dc:creator>
      <dc:date>2022-04-28T13:32:35Z</dc:date>
    </item>
    <item>
      <title>Re: sql question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sql-question/m-p/810396#M319578</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/65907"&gt;@wlierman&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You have not specified what is the error you are having OR how you want the error to be.&lt;BR /&gt;From the SQL code you have given, &amp;nbsp;I see that you have included the aggregate column (Contact_ID) in the select statement and your group statement does not include the non-aggregate columns. The result that comes in this scenario would be confusing or not what one would expect. I am giving a specimen code without the above two factors and you can see the difference.&lt;BR /&gt;Experiment a bit and I am sure you would get what you want.&lt;/P&gt;
&lt;P&gt;Lastly, a very important fact, please provide the source data as a datastep or atleast properly formatted. I have taken only three rows, try using the complete data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
Retain Contact_ID NAICS_Sector Sector_Type Date N;
informat Date ANYDTDTE9.;
Format date date9.;
length Sector_Type $ 20;
input Contact_ID NAICS_Sector $ Sector_Type $ Date N;
datalines;
1056200 GOODS AGRICULTURE_FORESTRY 10-Jun-20 1
1069490 GOODS AGRICULTURE_FORESTRY 13-Jun-20 1
1012517 GOODS MANUFACTURING 13-Jun-20 1
;
run;
Proc sql noprint;
CREATE Table want AS
   SELECT Date, Count(Contact_ID) as Number_of_contacts,NAICS_Sector, N
   FROM have
   Where '01APR20'd &amp;lt;= Date &amp;lt;= '31JAN22'd
   And NAICS_Sector = "GOODS"
   Group by date ,NAICS_Sector,N 
   Order by 1;
   quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The output would be like this&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Sajid01_0-1651159806871.png" style="width: 556px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/70947iB6FEC4F254289B0E/image-dimensions/556x137?v=v2" width="556" height="137" role="button" title="Sajid01_0-1651159806871.png" alt="Sajid01_0-1651159806871.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Apr 2022 15:33:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sql-question/m-p/810396#M319578</guid>
      <dc:creator>Sajid01</dc:creator>
      <dc:date>2022-04-28T15:33:02Z</dc:date>
    </item>
    <item>
      <title>Re: sql question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sql-question/m-p/810594#M319651</link>
      <description>&lt;P&gt;If you only want the date, and the count of the number of obsns for each date, you might try something like ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql noprint;&lt;/P&gt;
&lt;P&gt;create table foo as&lt;/P&gt;
&lt;P&gt;select unique date, count(*) as count&lt;/P&gt;
&lt;P&gt;from my_data&lt;/P&gt;
&lt;P&gt;group by date;&lt;/P&gt;
&lt;P&gt;quit; run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Apr 2022 11:59:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sql-question/m-p/810594#M319651</guid>
      <dc:creator>GraphGuy</dc:creator>
      <dc:date>2022-04-29T11:59:35Z</dc:date>
    </item>
  </channel>
</rss>

