<?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 Finding the first and last values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Finding-the-first-and-last-values/m-p/745064#M233512</link>
    <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to find the first and the last value of each row, giving back the variable's name, as I will try to explain in the next output:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Name&lt;/TD&gt;&lt;TD&gt;Source1&lt;/TD&gt;&lt;TD&gt;source2&lt;/TD&gt;&lt;TD&gt;source3&lt;/TD&gt;&lt;TD&gt;FirstValue&lt;/TD&gt;&lt;TD&gt;LastValue&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;a&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Source2&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;source3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;b&lt;/TD&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;Source1&lt;/TD&gt;&lt;TD&gt;Source2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;c&lt;/TD&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;source1&lt;/TD&gt;&lt;TD&gt;source3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;d&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;Source 2&lt;/TD&gt;&lt;TD&gt;source 3&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 02 Jun 2021 00:24:37 GMT</pubDate>
    <dc:creator>dmarques1998</dc:creator>
    <dc:date>2021-06-02T00:24:37Z</dc:date>
    <item>
      <title>Finding the first and last values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-the-first-and-last-values/m-p/745064#M233512</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to find the first and the last value of each row, giving back the variable's name, as I will try to explain in the next output:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Name&lt;/TD&gt;&lt;TD&gt;Source1&lt;/TD&gt;&lt;TD&gt;source2&lt;/TD&gt;&lt;TD&gt;source3&lt;/TD&gt;&lt;TD&gt;FirstValue&lt;/TD&gt;&lt;TD&gt;LastValue&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;a&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Source2&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;source3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;b&lt;/TD&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;Source1&lt;/TD&gt;&lt;TD&gt;Source2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;c&lt;/TD&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;source1&lt;/TD&gt;&lt;TD&gt;source3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;d&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;Source 2&lt;/TD&gt;&lt;TD&gt;source 3&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Jun 2021 00:24:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-the-first-and-last-values/m-p/745064#M233512</guid>
      <dc:creator>dmarques1998</dc:creator>
      <dc:date>2021-06-02T00:24:37Z</dc:date>
    </item>
    <item>
      <title>Re: Finding the first and last values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-the-first-and-last-values/m-p/745066#M233513</link>
      <description>&lt;P&gt;One Way :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	Name='a';
	source1=' ';
	source2='A';
	source3='B';
	output;
	Name='b';
	source1='A';
	source2='B';
	source3='';
	output;
	Name='c';
	source1='A';
	source2='';
	source3='B';
	output;
	Name='d';
	source1='';
	source2='A';
	source3='B';
	output;
run;

data want;
	set have;
	array src[*]  $ source:;
	do i=1 to dim(src);
		if ^ missing(src[i]) then
			do;
				FirstValue=vname(src[i]);
				leave;
			end;
	end;

	do  j=hbound(src) to 1 by -1;
		if ^ missing(src[j]) then
			do;
				LastValue=vname(src[j]);
				leave;
			end;
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 02 Jun 2021 01:13:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-the-first-and-last-values/m-p/745066#M233513</guid>
      <dc:creator>r_behata</dc:creator>
      <dc:date>2021-06-02T01:13:47Z</dc:date>
    </item>
    <item>
      <title>Re: Finding the first and last values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-the-first-and-last-values/m-p/745098#M233527</link>
      <description>&lt;P&gt;This is another example where bad data structure causes one to write unnecessarily complicated code.&lt;/P&gt;
&lt;P&gt;First, transpose your data to a long layout:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines dsd dlm=" " truncover;
input (Name source1 source2 source3) ($);
datalines;
a  A B 
b A B  
c A  B
d  A B
;

proc transpose data=have out=long (where=(col1 ne ""));
by name;
var source:;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now the exercise becomes very simple:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
retain name firstvalue; /* name included to keep variable order */
set long (rename=(_name_=lastvalue));
by name;
if first.name then firstvalue = lastvalue;
if last.name;
keep name firstvalue lastvalue;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 02 Jun 2021 08:23:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-the-first-and-last-values/m-p/745098#M233527</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-06-02T08:23:47Z</dc:date>
    </item>
    <item>
      <title>Re: Finding the first and last values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-the-first-and-last-values/m-p/745218#M233564</link>
      <description>It seems to be more easier. thank you. In the same example, if I have 12 different values for the same name, how can I keep only the first value?</description>
      <pubDate>Wed, 02 Jun 2021 15:29:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-the-first-and-last-values/m-p/745218#M233564</guid>
      <dc:creator>dmarques1998</dc:creator>
      <dc:date>2021-06-02T15:29:38Z</dc:date>
    </item>
    <item>
      <title>Re: Finding the first and last values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-the-first-and-last-values/m-p/745231#M233567</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/376436"&gt;@dmarques1998&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;It seems to be more easier. thank you. In the same example, if I have 12 different values for the same name, how can I keep only the first value?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That's even easier, as you do not need a RETAIN:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set long (rename=(_name_=firstvalue));
by name;
if first.name;
keep name firstvalue;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 02 Jun 2021 16:10:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-the-first-and-last-values/m-p/745231#M233567</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-06-02T16:10:36Z</dc:date>
    </item>
  </channel>
</rss>

