<?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: Chain lagged string variables by group-id and date in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Chain-lagged-string-variables-by-group-id-and-date/m-p/414397#M101531</link>
    <description>&lt;P&gt;Something like this?&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;data have;
input group_ID date:date9. staff_ID$;
format date date9.;
datalines;
1 31Jan91 AA
1 31Jan91 BB
1 31Jan91 CC
1 28Feb91 AA
1 28Feb91 CC
1 28Feb91 DD
2 31Jan91 GG
2 31Jan91 H
2 31Jan91 FF
2 28Feb91 TTT
2 28Feb91 D
2 28Feb91 Y
;

proc sort data=have;
	by group_ID date;
run;

data want;
	set have;
	length staff_ID_cum $20;
	by group_ID date;

	if first.date then staff_ID_cum = staff_ID;
	else staff_ID_cum = cats(staff_ID_cum, staff_ID);
	if last.date then output;

	retain staff_ID_cum;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 17 Nov 2017 14:44:49 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2017-11-17T14:44:49Z</dc:date>
    <item>
      <title>Chain lagged string variables by group-id and date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Chain-lagged-string-variables-by-group-id-and-date/m-p/414391#M101529</link>
      <description>&lt;P&gt;Dear community,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to ask for your help concerning the following issue.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The setting is as follows, with screenshots and sample data attached.&lt;/P&gt;&lt;P&gt;I have a list with a group id.&lt;/P&gt;&lt;P&gt;For every group id, I have multiple observations per date (monthly frequency).&lt;/P&gt;&lt;P&gt;For every group for every date, I have at least one staff id.&lt;/P&gt;&lt;P&gt;So e.g. for group id 1, on January 31st 1991, I observe three staff members with ids AA, BB and CC.&lt;/P&gt;&lt;P&gt;I want to chain them together, to have AABBCC. Afterward, I would just keep the row with only the chained IDs.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was messing around with "set by group_id date" and trying to use the lag function with cat(group_id, lag(group_id)), but I have proven to be too stupid.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It would be great, if you could please help me.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yours sincerely,&lt;/P&gt;&lt;P&gt;Sinistrum&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;have:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="have_group_staff.PNG" style="width: 262px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/16706iC46D61E539F3028F/image-size/large?v=v2&amp;amp;px=999" role="button" title="have_group_staff.PNG" alt="have_group_staff.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;want:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="want_group_staff.PNG" style="width: 326px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/16707iE496933560172208/image-size/large?v=v2&amp;amp;px=999" role="button" title="want_group_staff.PNG" alt="want_group_staff.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data	have;
	input
		group_ID	date staff_ID $;
		informat date date7.;
		format date ddmmyy10.;
	datalines;
1	31Jan91	AA
1	31Jan91	BB
1	31Jan91	CC
1	28Feb91	AA
1	28Feb91	CC
1	28Feb91	DD
2	31Jan91	GG
2	31Jan91	H
2	31Jan91	FF
2	28Feb91	TTT
2	28Feb91	D
2	28Feb91	Y
	;
run;
data	want;
	input
		group_ID	date staff_ID $	staff_ID_cum $;
		informat date date7.;
		format date ddmmyy10.;
	datalines;
1	31Jan91	AA	AA
1	31Jan91	BB	AABB
1	31Jan91	CC	AABBCC
1	28Feb91	AA	AA
1	28Feb91	CC	AACC
1	28Feb91	DD	AACCDD
2	31Jan91	GG	GG
2	31Jan91	H	GGH
2	31Jan91	FF	GGHFF
2	28Feb91	TTT	TTT
2	28Feb91	D	TTTD
2	28Feb91	Y	TTTDY
	;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Nov 2017 14:27:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Chain-lagged-string-variables-by-group-id-and-date/m-p/414391#M101529</guid>
      <dc:creator>Sinistrum</dc:creator>
      <dc:date>2017-11-17T14:27:53Z</dc:date>
    </item>
    <item>
      <title>Re: Chain lagged string variables by group-id and date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Chain-lagged-string-variables-by-group-id-and-date/m-p/414397#M101531</link>
      <description>&lt;P&gt;Something like this?&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;data have;
input group_ID date:date9. staff_ID$;
format date date9.;
datalines;
1 31Jan91 AA
1 31Jan91 BB
1 31Jan91 CC
1 28Feb91 AA
1 28Feb91 CC
1 28Feb91 DD
2 31Jan91 GG
2 31Jan91 H
2 31Jan91 FF
2 28Feb91 TTT
2 28Feb91 D
2 28Feb91 Y
;

proc sort data=have;
	by group_ID date;
run;

data want;
	set have;
	length staff_ID_cum $20;
	by group_ID date;

	if first.date then staff_ID_cum = staff_ID;
	else staff_ID_cum = cats(staff_ID_cum, staff_ID);
	if last.date then output;

	retain staff_ID_cum;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 17 Nov 2017 14:44:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Chain-lagged-string-variables-by-group-id-and-date/m-p/414397#M101531</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2017-11-17T14:44:49Z</dc:date>
    </item>
    <item>
      <title>Re: Chain lagged string variables by group-id and date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Chain-lagged-string-variables-by-group-id-and-date/m-p/414398#M101532</link>
      <description>&lt;P&gt;Something like:&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;
  retain staff_id_cum i;
  by group_id;
  if first.group_id then do;
    i=1;
    staff_id_cum=staff_id;
  end;
  else do;
    if i=3 then do;
      output;
      i=1;
      staff_id_cum=staff_id;
    end;
    else do;
      i=i+1;
      staff_id_cum=cats(staff_id_cum,staff_id);
    end;
  end;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Nov 2017 14:43:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Chain-lagged-string-variables-by-group-id-and-date/m-p/414398#M101532</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-11-17T14:43:30Z</dc:date>
    </item>
    <item>
      <title>Re: Chain lagged string variables by group-id and date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Chain-lagged-string-variables-by-group-id-and-date/m-p/414404#M101533</link>
      <description>&lt;P&gt;De facto, exactly like this - thank you very much&lt;/P&gt;&lt;P&gt;So quickly.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Your code, RW9, seems to delete too much observations as it leaves me with only two instead of the four (2 groups, 2 dates, implies 4 final observations to keep).&lt;/P&gt;&lt;P&gt;For date, it keeps the first one, though it posts the staff_id_cum for the first part, so there is a little mismatch, too.&lt;/P&gt;&lt;P&gt;Thank you either way.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yours sincerely&lt;/P&gt;&lt;P&gt;Sinistrum&lt;/P&gt;</description>
      <pubDate>Fri, 17 Nov 2017 15:07:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Chain-lagged-string-variables-by-group-id-and-date/m-p/414404#M101533</guid>
      <dc:creator>Sinistrum</dc:creator>
      <dc:date>2017-11-17T15:07:27Z</dc:date>
    </item>
    <item>
      <title>Re: Chain lagged string variables by group-id and date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Chain-lagged-string-variables-by-group-id-and-date/m-p/414407#M101535</link>
      <description>&lt;P&gt;Yes, I just read the first part as being 3 records per group, did not even see the date as a secondary grouping variable.&lt;/P&gt;</description>
      <pubDate>Fri, 17 Nov 2017 15:12:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Chain-lagged-string-variables-by-group-id-and-date/m-p/414407#M101535</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-11-17T15:12:10Z</dc:date>
    </item>
  </channel>
</rss>

