<?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: Table transformation in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Table-transformation/m-p/498440#M132456</link>
    <description>&lt;P&gt;Thanks for the solution and detailed solution, I was able to get the intended result.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 24 Sep 2018 15:24:13 GMT</pubDate>
    <dc:creator>samface</dc:creator>
    <dc:date>2018-09-24T15:24:13Z</dc:date>
    <item>
      <title>Table transformation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Table-transformation/m-p/498402#M132437</link>
      <description>&lt;P&gt;Good morning friends,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need help reconstructing the following table:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
input account_number time loan_amount default_status;
datalines;
410 201601 5000 0
410 201602 5000 0
410 201603 5000 1
411 201601 3000 0
411 201602 3000 0
411 201603 3000 0
412 201601 2500 1
412 201601 2500 0
412 201601 2500 0
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Using the above table, I want to generate a table that will return: 1.account_number, loan_amount, defaulted during the three months evaluation. For example, for account 410 since there one default, I want to obtain the values below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data result;&lt;BR /&gt;input account_number loan_amount defaulted;&lt;BR /&gt;datalines;&lt;BR /&gt;410 5000 1
411 3000 0
412 2500 1&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;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Sep 2018 14:13:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Table-transformation/m-p/498402#M132437</guid>
      <dc:creator>samface</dc:creator>
      <dc:date>2018-09-24T14:13:05Z</dc:date>
    </item>
    <item>
      <title>Re: Table transformation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Table-transformation/m-p/498408#M132441</link>
      <description>&lt;P&gt;Proc sql with group by and the max() summary function:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select
  account_number,
  loan_amount,
  max(default_status) as default_status
from test
group by
  account_number,
  loan_amount
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 24 Sep 2018 14:26:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Table-transformation/m-p/498408#M132441</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-09-24T14:26:18Z</dc:date>
    </item>
    <item>
      <title>Re: Table transformation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Table-transformation/m-p/498410#M132443</link>
      <description>&lt;P&gt;PROC SORT might work for you.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=test  ;
by account_number loan_amount descending default_status;
run; 
proc sort data=test out=want nodupkey;
by account_number loan_amount;
run; &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 24 Sep 2018 14:33:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Table-transformation/m-p/498410#M132443</guid>
      <dc:creator>SuryaKiran</dc:creator>
      <dc:date>2018-09-24T14:33:58Z</dc:date>
    </item>
    <item>
      <title>Re: Table transformation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Table-transformation/m-p/498411#M132444</link>
      <description>&lt;P&gt;I'd rather use a data step as the second step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=test;
by account_number loan_amount descending default_status;
run;

data want;
set test;
by account_number loan_amount;
if first.loan_amount;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 24 Sep 2018 14:37:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Table-transformation/m-p/498411#M132444</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-09-24T14:37:59Z</dc:date>
    </item>
    <item>
      <title>Re: Table transformation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Table-transformation/m-p/498418#M132446</link>
      <description>&lt;P&gt;First, thank you for providing a usable data step for the sample data.&amp;nbsp; It makes my heart sing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You appear to have only 3 monthly records per id, sorted by month within id.&amp;nbsp; So any instance of default status=1 means you assign default status=1 to the id.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is a minimalist programming solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
input account_number time loan_amount default_status;
datalines;
410 201601 5000 0
410 201602 5000 0
410 201603 5000 1
411 201601 3000 0
411 201602 3000 0
411 201603 3000 0
412 201601 2500 1
412 201601 2500 0
412 201601 2500 0
;
data want (drop=time);
  set test  test (where=(default_status=1));
  by account_number;
  if last.account_number;
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;The "trick" here is that a SET statement with a BY statement interleaves observations sorted by account_number.&amp;nbsp; That is all records from the first SET operand (i.e. all of test) precede all matching observations for the second SET operand (only defaults).&amp;nbsp; So if there are defaults the last record for an account will have default_status=1.&amp;nbsp; If there are no such records, then the account_number ends with a default=0 record.&amp;nbsp;&amp;nbsp; The "if last.account_number;" is a subsetting if, telling sas to keep only the final incoming record for each account.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note:&amp;nbsp; Using SET with by is usually done with different datasets&amp;nbsp; (e.g.&amp;nbsp; SET BOYS GIRLS; by id;), but can be very usefull in interleaving a dataset with itself, as above.&lt;/P&gt;</description>
      <pubDate>Mon, 24 Sep 2018 14:45:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Table-transformation/m-p/498418#M132446</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-09-24T14:45:45Z</dc:date>
    </item>
    <item>
      <title>Re: Table transformation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Table-transformation/m-p/498440#M132456</link>
      <description>&lt;P&gt;Thanks for the solution and detailed solution, I was able to get the intended result.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Sep 2018 15:24:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Table-transformation/m-p/498440#M132456</guid>
      <dc:creator>samface</dc:creator>
      <dc:date>2018-09-24T15:24:13Z</dc:date>
    </item>
    <item>
      <title>Re: Table transformation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Table-transformation/m-p/498441#M132457</link>
      <description>Thanks your code also works! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;</description>
      <pubDate>Mon, 24 Sep 2018 15:24:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Table-transformation/m-p/498441#M132457</guid>
      <dc:creator>samface</dc:creator>
      <dc:date>2018-09-24T15:24:55Z</dc:date>
    </item>
    <item>
      <title>Re: Table transformation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Table-transformation/m-p/498442#M132458</link>
      <description>Thanks this code solved my problem also.</description>
      <pubDate>Mon, 24 Sep 2018 15:25:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Table-transformation/m-p/498442#M132458</guid>
      <dc:creator>samface</dc:creator>
      <dc:date>2018-09-24T15:25:27Z</dc:date>
    </item>
  </channel>
</rss>

