<?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 to keep variables that are not in the SELECT DISTINCT statement in SAS SQL? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-that-are-not-in-the-SELECT-DISTINCT/m-p/363585#M86114</link>
    <description>&lt;P&gt;It would help if you can post example have and want datasets. On first glance it seems like you should be able to get what you want by simply including&amp;nbsp;&lt;SPAN&gt;shrout in your select statement. If you are looking to have single records for each "cusip" and "fyear" combination, but with multiple values of "shrout", I'd still do the same, but then apply a proc transpose to create the wide records.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Art, CEO, AnalystFinder.com&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 01 Jun 2017 20:15:02 GMT</pubDate>
    <dc:creator>art297</dc:creator>
    <dc:date>2017-06-01T20:15:02Z</dc:date>
    <item>
      <title>How to keep variables that are not in the SELECT DISTINCT statement in SAS SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-that-are-not-in-the-SELECT-DISTINCT/m-p/363583#M86113</link>
      <description>&lt;P&gt;I know how to get around this issue by writing an additional code, but I would like&amp;nbsp;to know whether I can select distinct observations based on the two variables "cusip" and "fyear" but still keep the variable "shrout" in the final dataset by the code below. The dataset&amp;nbsp;work.outstanding_shares_1 contains&amp;nbsp;shrout, cusip and fyear.&amp;nbsp;&lt;/P&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;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	create table work.outstanding_shares_2 as
	select distinct cusip, fyear 
	from work.outstanding_shares_1;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;Thanks for any suggestions.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Jun 2017 20:05:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-that-are-not-in-the-SELECT-DISTINCT/m-p/363583#M86113</guid>
      <dc:creator>Yegen</dc:creator>
      <dc:date>2017-06-01T20:05:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep variables that are not in the SELECT DISTINCT statement in SAS SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-that-are-not-in-the-SELECT-DISTINCT/m-p/363585#M86114</link>
      <description>&lt;P&gt;It would help if you can post example have and want datasets. On first glance it seems like you should be able to get what you want by simply including&amp;nbsp;&lt;SPAN&gt;shrout in your select statement. If you are looking to have single records for each "cusip" and "fyear" combination, but with multiple values of "shrout", I'd still do the same, but then apply a proc transpose to create the wide records.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Art, CEO, AnalystFinder.com&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Jun 2017 20:15:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-that-are-not-in-the-SELECT-DISTINCT/m-p/363585#M86114</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-06-01T20:15:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep variables that are not in the SELECT DISTINCT statement in SAS SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-that-are-not-in-the-SELECT-DISTINCT/m-p/363590#M86116</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13711"&gt;@art297&lt;/a&gt;, thanks for your reply.&lt;BR /&gt;&lt;BR /&gt;Here is a sample of the data:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.outstanding_shares_1;
infile cards expandtabs truncover;
input cusip	fyear shrout date : yymmdd10.;
format date yymmdd10.;
cards;
20910	1992	9284	19920831
20910	1992	9362	19921030
20910	1988	9002	19880331
20910	1988	9037	19881031
20910	1988	9023	19880729
36020	2006	12342	20061130
36020	2003	12541	20030930
36020	2006	12336	20060630
36020	2003	13031	20030131
36020	2003	12813	20031031
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;I want to select the most recent (and distinct) shrout for a given cusip and fyear. So the output will look like:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;cusip&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;fyear&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;shrout&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;20910&lt;/TD&gt;&lt;TD&gt;1988&lt;/TD&gt;&lt;TD&gt;9037&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;20910&lt;/TD&gt;&lt;TD&gt;1992&lt;/TD&gt;&lt;TD&gt;9362&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;36020&lt;/TD&gt;&lt;TD&gt;2003&lt;/TD&gt;&lt;TD&gt;12813&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;36020&lt;/TD&gt;&lt;TD&gt;2006&lt;/TD&gt;&lt;TD&gt;12342&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&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;</description>
      <pubDate>Thu, 01 Jun 2017 20:47:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-that-are-not-in-the-SELECT-DISTINCT/m-p/363590#M86116</guid>
      <dc:creator>Yegen</dc:creator>
      <dc:date>2017-06-01T20:47:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep variables that are not in the SELECT DISTINCT statement in SAS SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-that-are-not-in-the-SELECT-DISTINCT/m-p/363596#M86118</link>
      <description>I don't understand why you picked two obs for FY 2003 for cusip=36020</description>
      <pubDate>Thu, 01 Jun 2017 20:44:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-that-are-not-in-the-SELECT-DISTINCT/m-p/363596#M86118</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2017-06-01T20:44:15Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep variables that are not in the SELECT DISTINCT statement in SAS SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-that-are-not-in-the-SELECT-DISTINCT/m-p/363597#M86119</link>
      <description>&lt;P&gt;Sorry that was a typo,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15410"&gt;@data_null__&lt;/a&gt;. I will update the output table now. Thanks for pointing that out.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Jun 2017 20:46:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-that-are-not-in-the-SELECT-DISTINCT/m-p/363597#M86119</guid>
      <dc:creator>Yegen</dc:creator>
      <dc:date>2017-06-01T20:46:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep variables that are not in the SELECT DISTINCT statement in SAS SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-that-are-not-in-the-SELECT-DISTINCT/m-p/363598#M86120</link>
      <description>&lt;P&gt;Maybe this is what you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.outstanding_shares_1;
infile cards expandtabs truncover;
input cusip	fyear shrout date : yymmdd10.;
format date yymmdd10.;
cards;
20910	1992	9284	19920831
20910	1992	9362	19921030
20910	1988	9002	19880331
20910	1988	9037	19881031
20910	1988	9023	19880729
36020	2006	12342	20061130
36020	2003	12541	20030930
36020	2006	12336	20060630
36020	2003	13031	20030131
36020	2003	12813	20031031
;
run;
proc print;
   run;
proc summary nway;
   class cusip fyear;
   output out=shrout(drop=_:)
      idgroup(max(date) out(shrout)=);
   run;
proc print;
   run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;IMG title="2017-06-01_15-49-22.png" alt="2017-06-01_15-49-22.png" src="https://communities.sas.com/t5/image/serverpage/image-id/9187iD944BF9D0EC8CAA3/image-size/original?v=1.0&amp;amp;px=-1" border="0" /&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Jun 2017 20:51:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-that-are-not-in-the-SELECT-DISTINCT/m-p/363598#M86120</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2017-06-01T20:51:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep variables that are not in the SELECT DISTINCT statement in SAS SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-that-are-not-in-the-SELECT-DISTINCT/m-p/363600#M86121</link>
      <description>&lt;P&gt;Alternative SQL Method:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
	select distinct cusip, fyear, shrout 
	from work.outstanding_shares_1 
	where date in (select max(date) from work.outstanding_shares_1 group by cusip,fyear) ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 01 Jun 2017 20:55:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-that-are-not-in-the-SELECT-DISTINCT/m-p/363600#M86121</guid>
      <dc:creator>PBsas</dc:creator>
      <dc:date>2017-06-01T20:55:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep variables that are not in the SELECT DISTINCT statement in SAS SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-that-are-not-in-the-SELECT-DISTINCT/m-p/363602#M86122</link>
      <description>&lt;P&gt;Exactly! That is what I was looking for. Thanks!&lt;BR /&gt;&lt;BR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15410"&gt;@data_null__&lt;/a&gt;&amp;nbsp;(or even&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13711"&gt;@art297&lt;/a&gt;) do you know whether one can do the same by just using SQL? I was thinking of the having and group clause but got stuck. I wanted to try whether one can actually do this by just using SQL.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Jun 2017 20:56:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-that-are-not-in-the-SELECT-DISTINCT/m-p/363602#M86122</guid>
      <dc:creator>Yegen</dc:creator>
      <dc:date>2017-06-01T20:56:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep variables that are not in the SELECT DISTINCT statement in SAS SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-that-are-not-in-the-SELECT-DISTINCT/m-p/363605#M86123</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/53881"&gt;@PBsas&lt;/a&gt;&amp;nbsp;I just run your code, but the output does not look correct. I am having still duplicates for a given cusip &amp;amp; fyear.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Do you think one can do it with the "having" clause?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Jun 2017 21:01:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-that-are-not-in-the-SELECT-DISTINCT/m-p/363605#M86123</guid>
      <dc:creator>Yegen</dc:creator>
      <dc:date>2017-06-01T21:01:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep variables that are not in the SELECT DISTINCT statement in SAS SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-that-are-not-in-the-SELECT-DISTINCT/m-p/363612#M86126</link>
      <description>&lt;P&gt;Your sample data&amp;nbsp;produced right output using this code. Please provide sample data that produced duplicates.&lt;/P&gt;</description>
      <pubDate>Thu, 01 Jun 2017 21:08:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-that-are-not-in-the-SELECT-DISTINCT/m-p/363612#M86126</guid>
      <dc:creator>PBsas</dc:creator>
      <dc:date>2017-06-01T21:08:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep variables that are not in the SELECT DISTINCT statement in SAS SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-that-are-not-in-the-SELECT-DISTINCT/m-p/363628#M86133</link>
      <description>&lt;P&gt;You are right when I use your code on the inputted data, I am getting the correct result. However, when I use it on the actual data, I am getting a result as follows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/9191i08D8343DFD06CA95/image-size/original?v=1.0&amp;amp;px=-1" border="0" alt="Screen Shot 2017-06-01 at 5.47.17 PM.png" title="Screen Shot 2017-06-01 at 5.47.17 PM.png" /&gt;&lt;BR /&gt;&lt;BR /&gt;Here is the log:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; 1          OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
 61         
 62         proc sql;
 63         create table want as
 64         select distinct cusip, fyear, shrout
 65         from work.outstanding_shares_1
 66         where date in (select max(date) from work.outstanding_shares_1 group
 66       !  by cusip,fyear) ;
 NOTE: Table WORK.WANT created, with 168203 rows and 3 columns.
 
 67         quit;
 NOTE: PROCEDURE SQL used (Total process time):
       real time           0.20 seconds
       cpu time            0.34 seconds
       
 
 68         
 69         OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
 82         &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;The only difference of the actual dataset and the sample I provided is the date format:&lt;BR /&gt;&lt;BR /&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/9192iC34CB2F1A0AA696C/image-size/original?v=1.0&amp;amp;px=-1" border="0" alt="Screen Shot 2017-06-01 at 5.48.53 PM.png" title="Screen Shot 2017-06-01 at 5.48.53 PM.png" /&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Jun 2017 21:50:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-that-are-not-in-the-SELECT-DISTINCT/m-p/363628#M86133</guid>
      <dc:creator>Yegen</dc:creator>
      <dc:date>2017-06-01T21:50:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep variables that are not in the SELECT DISTINCT statement in SAS SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-that-are-not-in-the-SELECT-DISTINCT/m-p/363816#M86202</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.outstanding_shares_1;
infile cards expandtabs truncover;
input cusip	fyear shrout date : yymmdd10.;
format date yymmdd10.;
cards;
20910	1992	9284	19920831
20910	1992	9362	19921030
20910	1988	9002	19880331
20910	1988	9037	19881031
20910	1988	9023	19880729
36020	2006	12342	20061130
36020	2003	12541	20030930
36020	2006	12336	20060630
36020	2003	13031	20030131
36020	2003	12813	20031031
;
run;

proc sql;
select cusip,fyear,shrout
 from outstanding_shares_1
  group by cusip,fyear
   having date=max(date);
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 02 Jun 2017 14:05:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-that-are-not-in-the-SELECT-DISTINCT/m-p/363816#M86202</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-06-02T14:05:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep variables that are not in the SELECT DISTINCT statement in SAS SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-that-are-not-in-the-SELECT-DISTINCT/m-p/364426#M86441</link>
      <description>&lt;P&gt;Thanks,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;. Your help is much appreciated!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Jun 2017 00:45:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-variables-that-are-not-in-the-SELECT-DISTINCT/m-p/364426#M86441</guid>
      <dc:creator>Yegen</dc:creator>
      <dc:date>2017-06-06T00:45:49Z</dc:date>
    </item>
  </channel>
</rss>

