<?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: PROC SQL: Making one record out of many. in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-Making-one-record-out-of-many/m-p/8677#M414</link>
    <description>Due to the complexity of your conditions, I would recommend you NOT use proc sql, but use a data step.&lt;BR /&gt;
&lt;BR /&gt;
proc sort data=table; by id;&lt;BR /&gt;
data condensed;&lt;BR /&gt;
  set table;&lt;BR /&gt;
  by id;&lt;BR /&gt;
&lt;BR /&gt;
  retain min_var1 min_var3 max_var3 flag rem2a rem4a rem6a rem7a max_var2 rem3 rem2b rem4b rem6b rem7b avar5;&lt;BR /&gt;
&lt;BR /&gt;
  if first.id then do;&lt;BR /&gt;
    min_var1 = var1;&lt;BR /&gt;
    min_var3 = var3;&lt;BR /&gt;
    max_var3 = var3;&lt;BR /&gt;
    flag = 0;&lt;BR /&gt;
    rem2 = var2;&lt;BR /&gt;
    rem4 = var4;&lt;BR /&gt;
    rem6 = var6;&lt;BR /&gt;
    rem7 = var7;&lt;BR /&gt;
    max_var2 = var2;&lt;BR /&gt;
    rem3 = var3;&lt;BR /&gt;
    avar5 = 0;&lt;BR /&gt;
  end;&lt;BR /&gt;
&lt;BR /&gt;
  if var1 &amp;lt; min_var1 then min_var1 = var1;&lt;BR /&gt;
  if var3 &amp;lt; min_var3 then min_var3 = var3;&lt;BR /&gt;
&lt;BR /&gt;
  if var4 = 1 or var4 = 2 then do;&lt;BR /&gt;
    flag=1;&lt;BR /&gt;
    if var3 &amp;gt; max_var3 then max_var3 = var3;&lt;BR /&gt;
    rem2a = var2;&lt;BR /&gt;
    rem4a = var4;&lt;BR /&gt;
    rem6a = var6;&lt;BR /&gt;
    rem7a = var7;&lt;BR /&gt;
  end;&lt;BR /&gt;
  &lt;BR /&gt;
  if var2 &amp;gt; max_var2 then do;&lt;BR /&gt;
    rem3 = var3;&lt;BR /&gt;
    rem2b = var2;&lt;BR /&gt;
    rem4b = var4;&lt;BR /&gt;
    rem6b = var6;&lt;BR /&gt;
    rem7b = var7;&lt;BR /&gt;
  end;&lt;BR /&gt;
&lt;BR /&gt;
  if var5 = 'A' or var5 = 'B' then avar5 = 1;&lt;BR /&gt;
&lt;BR /&gt;
  if last.id then do;&lt;BR /&gt;
    var1 = min_var1;&lt;BR /&gt;
    avar3 = min_var3;&lt;BR /&gt;
    if flag = 1 then do;&lt;BR /&gt;
      bvar3 = max_var3;&lt;BR /&gt;
      var2 = rem2a;&lt;BR /&gt;
      var4 = rem4a;&lt;BR /&gt;
      var6 = rem6a;&lt;BR /&gt;
      var7 = rem7a;&lt;BR /&gt;
      output;&lt;BR /&gt;
    end;&lt;BR /&gt;
    if flag = 0 then do;&lt;BR /&gt;
      bvar3 = rem3;&lt;BR /&gt;
      var2 = rem2b;&lt;BR /&gt;
      var4 = rem4b;&lt;BR /&gt;
      var6 = rem6b;&lt;BR /&gt;
      var7 = rem7b;&lt;BR /&gt;
      output;&lt;BR /&gt;
    end;&lt;BR /&gt;
  end;&lt;BR /&gt;
  drop min_var1 min_var3 max_var3 rem2a rem4a rem6a rem7a max_var2 rem3 rem2b rem4b rem6b rem7b;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
The beauty of SAS is that you have so many ways to work with your data.  You have the choice of using SQL when it is appropriate -- for joins, selects, database access, etc. -- and data steps when it is appropriate -- reading files, parsing, complicated data selections and summaries like this case, data groupings difficult to implement in SQL, etc.&lt;BR /&gt;
&lt;BR /&gt;
"Use the right tool for the job" it's easier, quicker and the results are soooooo much better.</description>
    <pubDate>Fri, 30 Oct 2009 19:16:19 GMT</pubDate>
    <dc:creator>deleted_user</dc:creator>
    <dc:date>2009-10-30T19:16:19Z</dc:date>
    <item>
      <title>PROC SQL: Making one record out of many.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-Making-one-record-out-of-many/m-p/8674#M411</link>
      <description>My table contains 8 variables: ID, VAR1-VAR7.&lt;BR /&gt;
VAR5 is a character variable and the others are numerical.&lt;BR /&gt;
Each ID have several records. I want to have only one record for each ID in the following way:&lt;BR /&gt;
&lt;BR /&gt;
VAR1 = MIN(VAR1)&lt;BR /&gt;
MIN(VAR3) should be assigned to a new variable, lets say AVAR3=MIN(VAR3).&lt;BR /&gt;
&lt;BR /&gt;
If VAR4=1 or VAR4=2 in ANY of ID:s records, then I should find the MAX(VAR3) among those. That value should be assigned to a new variable, lets say BVAR3=MAX(VAR3) and VAR2, VAR4, VAR6 and VAR7 should be kept from the same record.&lt;BR /&gt;
&lt;BR /&gt;
If VAR4~=1 and VAR4~=2 in ALL of ID:s records, then I should find the MAX(&lt;B&gt;VAR2&lt;/B&gt;) among those. The assignment to BVAR3 then should be BVAR3=VAR3 from that record and VAR2, VAR4, VAR6 and VAR7 should be kept from the same record.&lt;BR /&gt;
&lt;BR /&gt;
If ANY of ID:s VAR5='A' or 'B' then I would like to assign the value 1 to the new numerical variable AVAR5, otherwise AVAR5 should have the value 0.&lt;BR /&gt;
&lt;BR /&gt;
Thanks for any help, with this rather complicated problem.</description>
      <pubDate>Wed, 28 Oct 2009 10:34:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-Making-one-record-out-of-many/m-p/8674#M411</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-10-28T10:34:30Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL: Making one record out of many.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-Making-one-record-out-of-many/m-p/8675#M412</link>
      <description>Without sample data I couldn't write test this, so consider it pseudo code.&lt;BR /&gt;
&lt;BR /&gt;
proc sort data = mytable;&lt;BR /&gt;
  by ID;&lt;BR /&gt;
run;&lt;BR /&gt;
proc sql;&lt;BR /&gt;
  create table Var3ORVar2 as&lt;BR /&gt;
  select ID,MAX(VAR4 = 1 or VAR4 = 2) as VAR4_1OR2, &lt;BR /&gt;
         max(var3) as BVAR3,&lt;BR /&gt;
         max(var2) as MaxVar2,&lt;BR /&gt;
         (VAR5 in ('A','B') as AVAR5&lt;BR /&gt;
    from mytable&lt;BR /&gt;
   group by ID;&lt;BR /&gt;
quit;&lt;BR /&gt;
data results(keep = ID BVAR3 AVAR5 VAR2 VAR4 VAR6 VAR7);&lt;BR /&gt;
  merge mytable Var3ORVar2;&lt;BR /&gt;
  by ID;&lt;BR /&gt;
  if VAR4_1OR2 then do;&lt;BR /&gt;
    if var3 = MaxVar3 then output;&lt;BR /&gt;
  end;&lt;BR /&gt;
  else do;&lt;BR /&gt;
    BVAR3 = var3;&lt;BR /&gt;
    if var2 = MaxVar2 then output;&lt;BR /&gt;
  end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
It could also be done in a single pass through the data using arrays or hashes, but I think the two step approach is best in this case.&lt;BR /&gt;
&lt;BR /&gt;
Curtis</description>
      <pubDate>Wed, 28 Oct 2009 14:23:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-Making-one-record-out-of-many/m-p/8675#M412</guid>
      <dc:creator>CurtisMack</dc:creator>
      <dc:date>2009-10-28T14:23:05Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL: Making one record out of many.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-Making-one-record-out-of-many/m-p/8676#M413</link>
      <description>I don't understand why you have &lt;B&gt;MAX&lt;/B&gt;(VAR4=1 or VAR4=2).&lt;BR /&gt;
&lt;BR /&gt;
VAR4 is a numerical variabel that can have any integer value. Depending on if VAR4 is in {1,2} in &lt;B&gt;any&lt;/B&gt; of ID:s records or not, different actions should be taken.&lt;BR /&gt;
&lt;BR /&gt;
&lt;B&gt;If&lt;/B&gt; VAR4 had been a character variable with possible values {q,w,e,r,t,y} and if depending on if VAR4 had been in {q,w} or not, you should have made different selections, how would you then replace MAX? The answer to that question could possibly clarify your use of MAX(VAR4=1 or VAR4=2).&lt;BR /&gt;
&lt;BR /&gt;
The VAR1=MIN(VAR1) is missing. Perhaps i should introduce a new variable AVAR1=MIN(VAR1).&lt;BR /&gt;
&lt;BR /&gt;
I would also like to sum the variable VAR7 from records where VAR3 le BVAR3; BVAR3 assigned depending on the value of VAR4, as described in my first message.&lt;BR /&gt;
&lt;BR /&gt;
Thanks for your help.</description>
      <pubDate>Thu, 29 Oct 2009 07:38:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-Making-one-record-out-of-many/m-p/8676#M413</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-10-29T07:38:38Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL: Making one record out of many.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-Making-one-record-out-of-many/m-p/8677#M414</link>
      <description>Due to the complexity of your conditions, I would recommend you NOT use proc sql, but use a data step.&lt;BR /&gt;
&lt;BR /&gt;
proc sort data=table; by id;&lt;BR /&gt;
data condensed;&lt;BR /&gt;
  set table;&lt;BR /&gt;
  by id;&lt;BR /&gt;
&lt;BR /&gt;
  retain min_var1 min_var3 max_var3 flag rem2a rem4a rem6a rem7a max_var2 rem3 rem2b rem4b rem6b rem7b avar5;&lt;BR /&gt;
&lt;BR /&gt;
  if first.id then do;&lt;BR /&gt;
    min_var1 = var1;&lt;BR /&gt;
    min_var3 = var3;&lt;BR /&gt;
    max_var3 = var3;&lt;BR /&gt;
    flag = 0;&lt;BR /&gt;
    rem2 = var2;&lt;BR /&gt;
    rem4 = var4;&lt;BR /&gt;
    rem6 = var6;&lt;BR /&gt;
    rem7 = var7;&lt;BR /&gt;
    max_var2 = var2;&lt;BR /&gt;
    rem3 = var3;&lt;BR /&gt;
    avar5 = 0;&lt;BR /&gt;
  end;&lt;BR /&gt;
&lt;BR /&gt;
  if var1 &amp;lt; min_var1 then min_var1 = var1;&lt;BR /&gt;
  if var3 &amp;lt; min_var3 then min_var3 = var3;&lt;BR /&gt;
&lt;BR /&gt;
  if var4 = 1 or var4 = 2 then do;&lt;BR /&gt;
    flag=1;&lt;BR /&gt;
    if var3 &amp;gt; max_var3 then max_var3 = var3;&lt;BR /&gt;
    rem2a = var2;&lt;BR /&gt;
    rem4a = var4;&lt;BR /&gt;
    rem6a = var6;&lt;BR /&gt;
    rem7a = var7;&lt;BR /&gt;
  end;&lt;BR /&gt;
  &lt;BR /&gt;
  if var2 &amp;gt; max_var2 then do;&lt;BR /&gt;
    rem3 = var3;&lt;BR /&gt;
    rem2b = var2;&lt;BR /&gt;
    rem4b = var4;&lt;BR /&gt;
    rem6b = var6;&lt;BR /&gt;
    rem7b = var7;&lt;BR /&gt;
  end;&lt;BR /&gt;
&lt;BR /&gt;
  if var5 = 'A' or var5 = 'B' then avar5 = 1;&lt;BR /&gt;
&lt;BR /&gt;
  if last.id then do;&lt;BR /&gt;
    var1 = min_var1;&lt;BR /&gt;
    avar3 = min_var3;&lt;BR /&gt;
    if flag = 1 then do;&lt;BR /&gt;
      bvar3 = max_var3;&lt;BR /&gt;
      var2 = rem2a;&lt;BR /&gt;
      var4 = rem4a;&lt;BR /&gt;
      var6 = rem6a;&lt;BR /&gt;
      var7 = rem7a;&lt;BR /&gt;
      output;&lt;BR /&gt;
    end;&lt;BR /&gt;
    if flag = 0 then do;&lt;BR /&gt;
      bvar3 = rem3;&lt;BR /&gt;
      var2 = rem2b;&lt;BR /&gt;
      var4 = rem4b;&lt;BR /&gt;
      var6 = rem6b;&lt;BR /&gt;
      var7 = rem7b;&lt;BR /&gt;
      output;&lt;BR /&gt;
    end;&lt;BR /&gt;
  end;&lt;BR /&gt;
  drop min_var1 min_var3 max_var3 rem2a rem4a rem6a rem7a max_var2 rem3 rem2b rem4b rem6b rem7b;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
The beauty of SAS is that you have so many ways to work with your data.  You have the choice of using SQL when it is appropriate -- for joins, selects, database access, etc. -- and data steps when it is appropriate -- reading files, parsing, complicated data selections and summaries like this case, data groupings difficult to implement in SQL, etc.&lt;BR /&gt;
&lt;BR /&gt;
"Use the right tool for the job" it's easier, quicker and the results are soooooo much better.</description>
      <pubDate>Fri, 30 Oct 2009 19:16:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-Making-one-record-out-of-many/m-p/8677#M414</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-10-30T19:16:19Z</dc:date>
    </item>
  </channel>
</rss>

