<?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 to get the variable which had the minimun date of observation from data step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-get-the-variable-which-had-the-minimun-date-of/m-p/592603#M169939</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines truncover;
input id (var_1-var_5)(:date9.);
format var_1-var_5 date9.;
datalines;
1 09Jan2011 . 11Jan2012 . . 
1 . 10Jan2011 11Jan2012 . . 
2 12Feb2012 11Feb2013 . . .
3 21Mar2014 16Mar2014 18Mar2015 .
4 . . 15Jan2016 10Feb2015 5Feb2015
;

data want(keep=id varname min_date);
    do until (last.id);
        set have;
        by id;
        array _ var_1-var_5;
        Min_date=min(Min_date, min(of var_1-var_5));
        var=max(var, whichn(Min_date, of var_1-var_5));
        varname=vname(_[var]);
    end;

    format min_date date9.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 30 Sep 2019 09:37:46 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2019-09-30T09:37:46Z</dc:date>
    <item>
      <title>how to get the variable which had the minimun date of observation from data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-get-the-variable-which-had-the-minimun-date-of/m-p/592597#M169934</link>
      <description>&lt;P&gt;Hello!&lt;/P&gt;&lt;P&gt;Could you please help me how i can get the variable which had the minimum date of observation from the list of variables (had more than 100 variables). The variables had either a missing or a specific date of observation. I know how to identify the min date for each ID by using min and array function but want to know the variable which had the minimum date of observation for each unique ID.One ID has only one observation, i.e not only the minimum date of observation but also the type of variable which had the minimum observation for each unique ID.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*Original Data structure;
data have;
input id var_1 var_2 var_3 var_4 var_5;
datalines;
1 10Jan2011 . 11Jan2012 . . 
2 12Feb2012 11Feb2013 . . .
3 21Mar2014 16Mar2014 18Mar2015 . .
4 . . 15Jan2016 10Feb2015 5Feb2015 
;

*wanted data structure;
 ID  Var    Min_date
 1   Var_1 10Jan2011
 2   Var_1 12Feb2012
 3   Var_2 16Mar2014
 4   var_5 5Feb2015&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thanks,&lt;/P&gt;</description>
      <pubDate>Mon, 30 Sep 2019 09:09:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-get-the-variable-which-had-the-minimun-date-of/m-p/592597#M169934</guid>
      <dc:creator>Abimal_Zippi</dc:creator>
      <dc:date>2019-09-30T09:09:35Z</dc:date>
    </item>
    <item>
      <title>Re: how to get the variable which had the minimun date of observation from data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-get-the-variable-which-had-the-minimun-date-of/m-p/592599#M169936</link>
      <description>&lt;P&gt;Please test your code before posting; as posted, there are only missing values.&lt;/P&gt;
&lt;P&gt;See this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id (var_1 var_2 var_3 var_4 var_5) (:date9.);
format var: date9.;
datalines;
1 10Jan2011 . 11Jan2012 . . 
2 12Feb2012 11Feb2013 . . .
3 21Mar2014 16Mar2014 18Mar2015 . .
4 . . 15Jan2016 10Feb2015 5Feb2015 
;

proc transpose data=have out=trans;
by id;
var var:;
run;

proc sort data=trans;
by id col1;
run;

data want;
set trans;
where col1 ne .;
by id;
if first.id;
rename _name_=var col1=min_date;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 30 Sep 2019 09:18:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-get-the-variable-which-had-the-minimun-date-of/m-p/592599#M169936</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-09-30T09:18:37Z</dc:date>
    </item>
    <item>
      <title>Re: how to get the variable which had the minimun date of observation from data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-get-the-variable-which-had-the-minimun-date-of/m-p/592603#M169939</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines truncover;
input id (var_1-var_5)(:date9.);
format var_1-var_5 date9.;
datalines;
1 09Jan2011 . 11Jan2012 . . 
1 . 10Jan2011 11Jan2012 . . 
2 12Feb2012 11Feb2013 . . .
3 21Mar2014 16Mar2014 18Mar2015 .
4 . . 15Jan2016 10Feb2015 5Feb2015
;

data want(keep=id varname min_date);
    do until (last.id);
        set have;
        by id;
        array _ var_1-var_5;
        Min_date=min(Min_date, min(of var_1-var_5));
        var=max(var, whichn(Min_date, of var_1-var_5));
        varname=vname(_[var]);
    end;

    format min_date date9.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 30 Sep 2019 09:37:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-get-the-variable-which-had-the-minimun-date-of/m-p/592603#M169939</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-09-30T09:37:46Z</dc:date>
    </item>
    <item>
      <title>Re: how to get the variable which had the minimun date of observation from data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-get-the-variable-which-had-the-minimun-date-of/m-p/592606#M169942</link>
      <description>That's great. Thanks a lot!.&lt;BR /&gt;One more question, the name of the variables are different, not like var_1, var_2.....can you advise me how can I use the transpose if the var names are different. Since I had more than 100, each with a different name.</description>
      <pubDate>Mon, 30 Sep 2019 09:42:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-get-the-variable-which-had-the-minimun-date-of/m-p/592606#M169942</guid>
      <dc:creator>Abimal_Zippi</dc:creator>
      <dc:date>2019-09-30T09:42:10Z</dc:date>
    </item>
    <item>
      <title>Re: how to get the variable which had the minimun date of observation from data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-get-the-variable-which-had-the-minimun-date-of/m-p/592609#M169944</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/177968"&gt;@Abimal_Zippi&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;That's great. Thanks a lot!.&lt;BR /&gt;One more question, the name of the variables are different, not like var_1, var_2.....can you advise me how can I use the transpose if the var names are different. Since I had more than 100, each with a different name.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If they form a close block in the dataset structure from left to right (no non-dates in between), then you can use this&lt;/P&gt;
&lt;PRE&gt;firstvar--lastvar&lt;/PRE&gt;
&lt;P&gt;to name the variables:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id (firstvar secondvar thirdvar fourthvar lastvar) (:date9.);
format firstvar--lastvar date9.;
datalines;
1 10Jan2011 . 11Jan2012 . . 
2 12Feb2012 11Feb2013 . . .
3 21Mar2014 16Mar2014 18Mar2015 . .
4 . . 15Jan2016 10Feb2015 5Feb2015 
;

proc transpose data=have out=trans;
by id;
var firstvar--lastvar;
run;

proc sort data=trans;
by id col1;
run;

data want;
set trans;
where col1 ne .;
by id;
if first.id;
rename _name_=var col1=min_date;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If such order is not the case, then you need to use an exhaustive list of variables; you could create such a list automatically from dictionary.columns:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select name into :varlist separated by ' '
from dictionary.columns
where libname = 'WORK' and memname = 'HAVE' and format = 'DATE9.';
quit;

proc transpose data=have out=trans;
by id;
var &amp;amp;varlist.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Sep 2019 10:16:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-get-the-variable-which-had-the-minimun-date-of/m-p/592609#M169944</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-09-30T10:16:25Z</dc:date>
    </item>
  </channel>
</rss>

