<?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 Concatenating same field by id in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Concatenating-same-field-by-id/m-p/64986#M14113</link>
    <description>I need the last 5 teachers' names concatenated for each course_code.  At the end, I'd only have one record for each course_code that would contain course_code and teachers_names.  &lt;BR /&gt;
&lt;BR /&gt;
I thought I'd use PROC RANK to get the last 5 records for each course_code (using begin_date field).  Now, how do I concatenate the names?  Is there any other way of doing the whole thing more efficiently?   &lt;BR /&gt;
&lt;BR /&gt;
Dateset: sasuser.schedule&lt;BR /&gt;
Course_Code  Location	Begin_Date	Teacher&lt;BR /&gt;
C001	Seattle	23-Oct-00	Hallis, Dr. George&lt;BR /&gt;
C002	Dallas	4-Dec-00	Wickam, Dr. Alice&lt;BR /&gt;
C003	Boston	8-Jan-01	Forest, Mr. Peter&lt;BR /&gt;
C004	Seattle	22-Jan-01	Tally, Ms. Julia&lt;BR /&gt;
C005	Dallas	26-Feb-01	Hallis, Dr. George&lt;BR /&gt;
C006	Boston	2-Apr-01	Berthan, Ms. Judy&lt;BR /&gt;
C001	Dallas	21-May-01	Hallis, Dr. George</description>
    <pubDate>Sun, 16 Aug 2009 02:42:23 GMT</pubDate>
    <dc:creator>deleted_user</dc:creator>
    <dc:date>2009-08-16T02:42:23Z</dc:date>
    <item>
      <title>Concatenating same field by id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenating-same-field-by-id/m-p/64986#M14113</link>
      <description>I need the last 5 teachers' names concatenated for each course_code.  At the end, I'd only have one record for each course_code that would contain course_code and teachers_names.  &lt;BR /&gt;
&lt;BR /&gt;
I thought I'd use PROC RANK to get the last 5 records for each course_code (using begin_date field).  Now, how do I concatenate the names?  Is there any other way of doing the whole thing more efficiently?   &lt;BR /&gt;
&lt;BR /&gt;
Dateset: sasuser.schedule&lt;BR /&gt;
Course_Code  Location	Begin_Date	Teacher&lt;BR /&gt;
C001	Seattle	23-Oct-00	Hallis, Dr. George&lt;BR /&gt;
C002	Dallas	4-Dec-00	Wickam, Dr. Alice&lt;BR /&gt;
C003	Boston	8-Jan-01	Forest, Mr. Peter&lt;BR /&gt;
C004	Seattle	22-Jan-01	Tally, Ms. Julia&lt;BR /&gt;
C005	Dallas	26-Feb-01	Hallis, Dr. George&lt;BR /&gt;
C006	Boston	2-Apr-01	Berthan, Ms. Judy&lt;BR /&gt;
C001	Dallas	21-May-01	Hallis, Dr. George</description>
      <pubDate>Sun, 16 Aug 2009 02:42:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenating-same-field-by-id/m-p/64986#M14113</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-08-16T02:42:23Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenating same field by id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenating-same-field-by-id/m-p/64987#M14114</link>
      <description>Using a DATA step, declare a "large enough" SAS variable to hold all names, also code a RETAIN statement to retain values across DATA step iterations, and use an assignment statement to concatenate values, with TRIM or CATT function.  &lt;BR /&gt;
&lt;BR /&gt;
Also, to be able to count the last five observations for a given "by group", you will need to do something like sort your file using a particular BY statement group, then assign an OBS_NUMBER variable.  And then sort by DESCENDING OBS_NUM, and use this file to get your "last 'nn' observations".&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Sun, 16 Aug 2009 12:34:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenating-same-field-by-id/m-p/64987#M14114</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2009-08-16T12:34:47Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenating same field by id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenating-same-field-by-id/m-p/64988#M14115</link>
      <description>You don't mention the criteria for determining the LAST 5.  So I choose DATE.  The IDGROUP option in PROC SUMMARY makes this very easy, assuming that this is what you want.  Then you just use CATX to concatenate the 5 new variables.&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
data sched;&lt;BR /&gt;
   input &lt;BR /&gt;
      Course_Code:$4.&lt;BR /&gt;
      Location :$16. &lt;BR /&gt;
      Begin_Date:date. &lt;BR /&gt;
      Teacher &amp;amp;$32.;&lt;BR /&gt;
   format BEG: date9.;&lt;BR /&gt;
   cards;&lt;BR /&gt;
C001 Seattle 23-Oct-00 Hallis, Dr. George&lt;BR /&gt;
C002 Dallas 4-Dec-00 Wickam, Dr. Alice&lt;BR /&gt;
C003 Boston 8-Jan-01 Forest, Mr. Peter&lt;BR /&gt;
C004 Seattle 22-Jan-01 Tally, Ms. Julia&lt;BR /&gt;
C005 Dallas 26-Feb-01 Hallis, Dr. George&lt;BR /&gt;
C006 Boston 2-Apr-01 Berthan, Ms. Judy&lt;BR /&gt;
C001 Dallas 21-May-01 Hallis, Dr. George &lt;BR /&gt;
;;;;&lt;BR /&gt;
   run;&lt;BR /&gt;
proc summary nway;&lt;BR /&gt;
   class course:;&lt;BR /&gt;
   output out=wide(drop=_:)&lt;BR /&gt;
      idgroup(max(Beg:) out[5](teacher)=);&lt;BR /&gt;
   Run;&lt;BR /&gt;
data wide;&lt;BR /&gt;
   set wide;&lt;BR /&gt;
   length teachers $256;&lt;BR /&gt;
   teachers = catx('; ', of teacher_:);&lt;BR /&gt;
   drop teacher_:;&lt;BR /&gt;
   run;&lt;BR /&gt;
proc print;&lt;BR /&gt;
   run;&lt;BR /&gt;
[/pre]</description>
      <pubDate>Mon, 17 Aug 2009 13:17:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenating-same-field-by-id/m-p/64988#M14115</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2009-08-17T13:17:21Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenating same field by id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenating-same-field-by-id/m-p/64989#M14116</link>
      <description>Thanks sbb and data _null_ for your help.  Data _null_, it was an unique way by the way.&lt;BR /&gt;
&lt;BR /&gt;
The following is based on sbb's idea:&lt;BR /&gt;
&lt;BR /&gt;
*method 2;&lt;BR /&gt;
proc sort data=sasuser.schedule out=schedule; by course_code; run;&lt;BR /&gt;
Data schedule; &lt;BR /&gt;
    set schedule(in=a) schedule(in=b); &lt;BR /&gt;
    by course_code; &lt;BR /&gt;
    if a then do; &lt;BR /&gt;
        Length teachers $ 200 ; &lt;BR /&gt;
        if first.course_code then teachers=''; &lt;BR /&gt;
        teachers=catx('',teachers,teacher); &lt;BR /&gt;
        retain teachers; put _all_ '='; &lt;BR /&gt;
    end; &lt;BR /&gt;
    if b;&lt;BR /&gt;
	keep course_code teachers;&lt;BR /&gt;
Run; &lt;BR /&gt;
&lt;BR /&gt;
proc sort data=schedule nodupkey; by course_code; run;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
One can perform the task using PROC TRANSPOSE also.  The following is the code:&lt;BR /&gt;
*method3;&lt;BR /&gt;
proc sort data=sasuser.schedule out=schedule; by course_code; run;&lt;BR /&gt;
proc transpose data=schedule &lt;BR /&gt;
	out=schedule; &lt;BR /&gt;
	var teacher;&lt;BR /&gt;
	by course_code;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
data schedule;   &lt;BR /&gt;
	set schedule;   length teachers $256;   &lt;BR /&gt;
	teachers = catx('', COL1, COL2, COL3, COL4, COL5);   &lt;BR /&gt;
	keep course_code teachers;   &lt;BR /&gt;
run;</description>
      <pubDate>Tue, 18 Aug 2009 03:08:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenating-same-field-by-id/m-p/64989#M14116</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-08-18T03:08:14Z</dc:date>
    </item>
  </channel>
</rss>

