<?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: How do I generate a variable for each observation of each ID? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-generate-a-variable-for-each-observation-of-each-ID/m-p/610473#M177780</link>
    <description>&lt;P&gt;Thanks - you too :-).&lt;/P&gt;</description>
    <pubDate>Mon, 09 Dec 2019 14:06:26 GMT</pubDate>
    <dc:creator>niki0209</dc:creator>
    <dc:date>2019-12-09T14:06:26Z</dc:date>
    <item>
      <title>How do I generate a variable for each observation of each ID?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-generate-a-variable-for-each-observation-of-each-ID/m-p/610451#M177770</link>
      <description>&lt;P&gt;I have a dataset where the same individuals typically have multiple observations. Like this:&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; |&amp;nbsp; &amp;nbsp; Startdate&amp;nbsp; &amp;nbsp; |&amp;nbsp; &amp;nbsp; Enddate&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01/01/2001&amp;nbsp; &amp;nbsp; &amp;nbsp; 31/12/2001&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01/01/2002&amp;nbsp; &amp;nbsp; &amp;nbsp; 04/05/2002&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;06/07/2002&amp;nbsp; &amp;nbsp; &amp;nbsp; 30/11/2002&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;05/05/2001&amp;nbsp; &amp;nbsp; &amp;nbsp; 05/06/2001&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;07/07/2001&amp;nbsp; &amp;nbsp; &amp;nbsp; 15/10/2001&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01/04/2002&amp;nbsp; &amp;nbsp; &amp;nbsp; 08/09/2003&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I want is for the data to be transformed, so there is only one observation for each individual, like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; |&amp;nbsp; &amp;nbsp; Startdate1&amp;nbsp; &amp;nbsp; |&amp;nbsp; &amp;nbsp; Enddate1&amp;nbsp; &amp;nbsp; |&amp;nbsp; &amp;nbsp; Startdate2&amp;nbsp; &amp;nbsp; |&amp;nbsp; &amp;nbsp; Enddate2&amp;nbsp; &amp;nbsp; |&amp;nbsp; &amp;nbsp; Startdate3&amp;nbsp; &amp;nbsp; |&amp;nbsp; &amp;nbsp; Enddate3&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;01/01/2001&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;31/12/2001&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 01/01/2002&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;04/05/2002&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;06/07/2002&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;30/11/2002&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;05/05/2001&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;05/06/2001&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;07/07/2001&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;15/10/2001&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;01/04/2002&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;08/09/2003&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The actual dataset contains hundreds of thousands of observations, so I need a "general" code for conpressing observations for the same individual without losing any observations.&lt;/P&gt;&lt;P&gt;Can someone help?&lt;/P&gt;</description>
      <pubDate>Mon, 09 Dec 2019 13:14:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-generate-a-variable-for-each-observation-of-each-ID/m-p/610451#M177770</guid>
      <dc:creator>niki0209</dc:creator>
      <dc:date>2019-12-09T13:14:47Z</dc:date>
    </item>
    <item>
      <title>Re: How do I generate a variable for each observation of each ID?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-generate-a-variable-for-each-observation-of-each-ID/m-p/610466#M177777</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/302884"&gt;@niki0209&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can ty this, using an array function:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input ID Startdate Enddate;
	informat Startdate Enddate ddmmyy10.;
	format Startdate Enddate ddmmyy10.;
	cards;
1 01/01/2001 31/12/2001
1 01/01/2002 04/05/2002
1 06/07/2002 30/11/2002
2 05/05/2001 05/06/2001
2 07/07/2001 15/10/2001
3 01/04/2002 08/09/2003
;
run;

proc sql noprint;
	select max(nb_obs) into:nb_obs from (select count(*) as nb_obs from have group by ID);
quit;

data want;

	set have (rename=(Startdate=_Startdate Enddate=_Enddate));

	array Startdate(&amp;amp;nb_obs);
	array Enddate(&amp;amp;nb_obs);

	by ID;
	retain Startdate;
	retain Enddate;
	
 	if first.ID then do;
	 		counter=0;
	 		call missing (of Startdate(*));
 			call missing (of Enddate(*));
 		end;

 	counter+1;
	
	Startdate(counter) = _Startdate;
	Enddate(counter) = _Enddate;
		
	if last.ID then output;
	
	drop _Startdate _Enddate counter;
	format Startdate: Enddate: ddmmyy10.;

run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 09 Dec 2019 13:40:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-generate-a-variable-for-each-observation-of-each-ID/m-p/610466#M177777</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2019-12-09T13:40:44Z</dc:date>
    </item>
    <item>
      <title>Re: How do I generate a variable for each observation of each ID?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-generate-a-variable-for-each-observation-of-each-ID/m-p/610469#M177778</link>
      <description>&lt;P&gt;Thanks! Completely solves my problem :-).&lt;/P&gt;</description>
      <pubDate>Mon, 09 Dec 2019 14:03:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-generate-a-variable-for-each-observation-of-each-ID/m-p/610469#M177778</guid>
      <dc:creator>niki0209</dc:creator>
      <dc:date>2019-12-09T14:03:05Z</dc:date>
    </item>
    <item>
      <title>Re: How do I generate a variable for each observation of each ID?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-generate-a-variable-for-each-observation-of-each-ID/m-p/610471#M177779</link>
      <description>&lt;P&gt;You're welcome &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/302884"&gt;@niki0209&lt;/a&gt;&amp;nbsp;!&lt;/P&gt;
&lt;P&gt;Have a great day&amp;nbsp;&lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://communities.sas.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Dec 2019 14:05:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-generate-a-variable-for-each-observation-of-each-ID/m-p/610471#M177779</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2019-12-09T14:05:09Z</dc:date>
    </item>
    <item>
      <title>Re: How do I generate a variable for each observation of each ID?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-generate-a-variable-for-each-observation-of-each-ID/m-p/610473#M177780</link>
      <description>&lt;P&gt;Thanks - you too :-).&lt;/P&gt;</description>
      <pubDate>Mon, 09 Dec 2019 14:06:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-generate-a-variable-for-each-observation-of-each-ID/m-p/610473#M177780</guid>
      <dc:creator>niki0209</dc:creator>
      <dc:date>2019-12-09T14:06:26Z</dc:date>
    </item>
    <item>
      <title>Re: How do I generate a variable for each observation of each ID?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-generate-a-variable-for-each-observation-of-each-ID/m-p/610699#M177888</link>
      <description>&lt;P&gt;MERGE skill.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input ID Startdate Enddate;
	informat Startdate Enddate ddmmyy10.;
	format Startdate Enddate ddmmyy10.;
	cards;
1 01/01/2001 31/12/2001
1 01/01/2002 04/05/2002
1 06/07/2002 30/11/2002
2 05/05/2001 05/06/2001
2 07/07/2001 15/10/2001
3 01/04/2002 08/09/2003
;
run;

data temp;
 set have;
 by id;
 if first.id then n=0;
 n+1;
run;
proc sql noprint;
select distinct catt('temp(where=(n=',n,')
rename=(Startdate=Startdate_',n,' Enddate=Enddate_',n,'))')	into : merge separated by ' '
 from temp ;
quit;
data want;
 merge &amp;amp;merge;
 by id;
 drop n;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 10 Dec 2019 12:46:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-generate-a-variable-for-each-observation-of-each-ID/m-p/610699#M177888</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-12-10T12:46:04Z</dc:date>
    </item>
    <item>
      <title>Re: How do I generate a variable for each observation of each ID?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-generate-a-variable-for-each-observation-of-each-ID/m-p/624141#M183805</link>
      <description>&lt;P&gt;A double transpose can reshape the data into the form you desire.&amp;nbsp; A double transpose is needed because two variables are being pivoted into columns.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
attrib id length=8 startdate enddate informat=ddmmyy10. format=ddmmyy10.;
input ID  Startdate    Enddate; datalines;
1         01/01/2001      31/12/2001
1         01/01/2002      04/05/2002
1         06/07/2002      30/11/2002
2         05/05/2001      05/06/2001
2         07/07/2001      15/10/2001
3         01/04/2002      08/09/2003
;

data have_view / view=have_view;    
  set have;
  by id;
  if first.id then seq=1; else seq+1;
run;

proc transpose data=have_view out=name_value;
  by id seq; 
  var startdate enddate;
run;

proc transpose data=name_value out=want(drop=_name_);
  by id;
  id _name_ seq;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;A second way is to perform an 'array-based transposed'.&amp;nbsp; This requires&amp;nbsp;&lt;EM&gt;an apriori scan&lt;/EM&gt; of the data to find the size of the largest id group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  select max (subselect.freq) into :across_count trimmed from 
  (select id, count(*) as freq from have group by id) as subselect;

%put NOTE: &amp;amp;=across_count;

data want2;
  do _n_ = 1 by 1 until (last.id);
    set have;
    by id;

    array starts startdate1-startdate&amp;amp;across_count;  
    array ends   enddate1  -enddate&amp;amp;across_count;

    starts [_n_] = startdate;
    ends   [_n_] = enddate;
  end;

  attrib startdate: enddate: format=ddmmyy10. informat=ddmmyy10.;
  drop startdate enddate;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want the array based solution to have a column order that is like start1, end1, start2, end2, etc... you will need a macro to generate the variable names in that order.&amp;nbsp; Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro enum(min=1, max=, template=);
  %local index;
  %do index = &amp;amp;min %to &amp;amp;max;
    %unquote(&amp;amp;template)
  %end;
%mend;

data want3;
  do _n_ = 1 by 1 until (last.id);
    set have;
    by id;

    attrib 
      %enum(max=&amp;amp;across_count, template=%nrstr(startdate&amp;amp;index enddate&amp;amp;index))
      format=ddmmyy10. informat=ddmmyy10.
    ;

    array starts startdate1-startdate&amp;amp;across_count;  
    array ends   enddate1  -enddate&amp;amp;across_count;

    starts [_n_] = startdate;
    ends   [_n_] = enddate;
  end;

  drop startdate enddate;
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;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Feb 2020 11:05:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-generate-a-variable-for-each-observation-of-each-ID/m-p/624141#M183805</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2020-02-12T11:05:49Z</dc:date>
    </item>
  </channel>
</rss>

