<?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: Collapsing multiple records in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Collapsing-multiple-records/m-p/636658#M189167</link>
    <description>&lt;P&gt;Thanks Leonid, this is perfect.&lt;/P&gt;</description>
    <pubDate>Wed, 01 Apr 2020 19:52:58 GMT</pubDate>
    <dc:creator>DavidKaib</dc:creator>
    <dc:date>2020-04-01T19:52:58Z</dc:date>
    <item>
      <title>Collapsing multiple records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Collapsing-multiple-records/m-p/636631#M189157</link>
      <description>&lt;P&gt;I have seen various things that are close to this, but not exactly was I am trying to do. I have a data set where there are anywhere from 1 to 3 records per person. There are some fields that are identical in multiple records (firstname, lastname, gender and id) and others that are not (degree and major). I know which fields are invariant and which ones are different. I am hoping to find code to collapse the records so there is only one per person, where the fields that have multiple values can be put into a string (BA;MA) and the ones that do not remain the same. Is there a way to do this? I'm using SAS base 9.4.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So this:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;FirstName&lt;/TD&gt;&lt;TD&gt;LastName&lt;/TD&gt;&lt;TD&gt;Gender&lt;/TD&gt;&lt;TD&gt;Degree&lt;/TD&gt;&lt;TD&gt;Major&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1234567&lt;/TD&gt;&lt;TD&gt;Bill&lt;/TD&gt;&lt;TD&gt;Johnson&lt;/TD&gt;&lt;TD&gt;M&lt;/TD&gt;&lt;TD&gt;BA&lt;/TD&gt;&lt;TD&g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D&gt;&lt;TD&gt;M&lt;/TD&gt;&lt;TD&gt;BA&lt;/TD&gt;&lt;TD&gt;Sociology&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2345678&lt;/TD&gt;&lt;TD&gt;Jen&lt;/TD&gt;&lt;TD&gt;Baker&lt;/TD&gt;&lt;TD&gt;W&lt;/TD&gt;&lt;TD&gt;BA;MA&lt;/TD&gt;&lt;TD&gt;Sociology;Psychology&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4567890&lt;/TD&gt;&lt;TD&gt;Stan&lt;/TD&gt;&lt;TD&gt;Wright&lt;/TD&gt;&lt;TD&gt;M&lt;/TD&gt;&lt;TD&gt;MA;MA&lt;/TD&gt;&lt;TD&gt;Psychology;Sociology&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Apr 2020 18:42:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Collapsing-multiple-records/m-p/636631#M189157</guid>
      <dc:creator>DavidKaib</dc:creator>
      <dc:date>2020-04-01T18:42:39Z</dc:date>
    </item>
    <item>
      <title>Re: Collapsing multiple records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Collapsing-multiple-records/m-p/636633#M189159</link>
      <description>&lt;P&gt;Do you have a known maximum number of records that will need to collapse? This is needed so that you can create variables long enough to hold the results.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And another question, what exactly will you be able to do with the resulting file that can't be done with the existing one?&lt;/P&gt;</description>
      <pubDate>Wed, 01 Apr 2020 18:57:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Collapsing-multiple-records/m-p/636633#M189159</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-04-01T18:57:29Z</dc:date>
    </item>
    <item>
      <title>Re: Collapsing multiple records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Collapsing-multiple-records/m-p/636636#M189161</link>
      <description>&lt;P&gt;I would like to ask a similar question to that of&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What could you possibly do with text fields "BA;MA" or "Sociology;Psychology"? how would that help whatever comes next.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Maybe you want a column for each student named BA that is either 1 or 0, and another column for each student named MA which is 1 or 0; and so on. That could be a better form for subsequent analysis or reporting.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Apr 2020 19:06:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Collapsing-multiple-records/m-p/636636#M189161</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-04-01T19:06:59Z</dc:date>
    </item>
    <item>
      <title>Re: Collapsing multiple records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Collapsing-multiple-records/m-p/636639#M189163</link>
      <description>&lt;P&gt;The variables won't end up being too long. I am losing a legacy way of generating the data set that did this for me, and that I used within SAS, and this hasn't been a problem. The new source cannot collaspe the records, so I am basically trying to replicate what I have previously had.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The resulting file is used as a contact list for a survey, and then later for analysis of the survey. In both cases I need an unduplicated count with all the information.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Apr 2020 19:14:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Collapsing-multiple-records/m-p/636639#M189163</guid>
      <dc:creator>DavidKaib</dc:creator>
      <dc:date>2020-04-01T19:14:51Z</dc:date>
    </item>
    <item>
      <title>Re: Collapsing multiple records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Collapsing-multiple-records/m-p/636642#M189164</link>
      <description>&lt;P&gt;&lt;A href="https://gist.github.com/statgeek/d583cfa992bf56da51d435165b07e96a" target="_blank"&gt;https://gist.github.com/statgeek/d583cfa992bf56da51d435165b07e96a&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;This provides two examples. If you're combining more than one field, using the data step approach maybe easier/less steps.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Apr 2020 19:17:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Collapsing-multiple-records/m-p/636642#M189164</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-04-01T19:17:18Z</dc:date>
    </item>
    <item>
      <title>Re: Collapsing multiple records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Collapsing-multiple-records/m-p/636643#M189165</link>
      <description>&lt;P&gt;Length was never the issue, in my mind. Creating a data set that would be easy to use and easy to program in subsequent analysis is the issue, and the data set I described with BA 0 or 1 and MA 0 or 1 and so on seems like an easier data set to work with.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Apr 2020 19:18:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Collapsing-multiple-records/m-p/636643#M189165</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-04-01T19:18:44Z</dc:date>
    </item>
    <item>
      <title>Re: Collapsing multiple records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Collapsing-multiple-records/m-p/636649#M189166</link>
      <description>&lt;P&gt;Hi &lt;A class="trigger-hovercard" style="color: #999999;" href="https://communities.sas.com/t5/user/viewprofilepage/user-id/320110" target="_blank"&gt;DavidKaib&lt;/A&gt;,&lt;/P&gt;
&lt;P&gt;I believe you can do it using the following code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=A out=A1 (rename=(Degree=D Major=M));
   by ID;
run;

data B (drop=D M);
   set A1;
   by ID;
   length Degree $20 Major $90;
   retain Degree Major;
   if first.ID then do;
      Degree = D;
      Major = M;
   end;
   else do; 
      Degree = catx(';',Degree,D);
      Major  = catx(';',Major, M);
   end;
   if last.ID then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here A is your first data set, B is your destination data set. At least, this gives you an idea.&lt;/P&gt;
&lt;P&gt;Hope this helps.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Apr 2020 19:26:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Collapsing-multiple-records/m-p/636649#M189166</guid>
      <dc:creator>LeonidBatkhan</dc:creator>
      <dc:date>2020-04-01T19:26:01Z</dc:date>
    </item>
    <item>
      <title>Re: Collapsing multiple records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Collapsing-multiple-records/m-p/636658#M189167</link>
      <description>&lt;P&gt;Thanks Leonid, this is perfect.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Apr 2020 19:52:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Collapsing-multiple-records/m-p/636658#M189167</guid>
      <dc:creator>DavidKaib</dc:creator>
      <dc:date>2020-04-01T19:52:58Z</dc:date>
    </item>
    <item>
      <title>Re: Collapsing multiple records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Collapsing-multiple-records/m-p/636663#M189169</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID $ FirstName $ LastName $ Gender $ Degree $ Major  $10.;
cards;
1234567 Bill Johnson M BA Sociology 
2345678 Jen Baker W BA Sociology 
2345678 Jen Baker W MA Psychology 
4567890 Stan Wright M MA Psychology 
4567890 Stan Wright M MA Sociology 
;;;;
run;


data want(rename=(Degree_=Degree Major_=Major));
length Major_ $50. Degree_ $25.;
	do until(last.id);
		set have;
		by id;
		Degree_ = catx(';',Degree_,Degree); 
		Major_=catx(';',Major_,major); 
	end;
	drop major Degree;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 01 Apr 2020 20:10:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Collapsing-multiple-records/m-p/636663#M189169</guid>
      <dc:creator>r_behata</dc:creator>
      <dc:date>2020-04-01T20:10:31Z</dc:date>
    </item>
  </channel>
</rss>

