<?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: Merge in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Merge/m-p/339716#M77529</link>
    <description>Just been curious. In real world what are you trying to achieve with this ?</description>
    <pubDate>Thu, 09 Mar 2017 17:36:00 GMT</pubDate>
    <dc:creator>anoopmohandas7</dc:creator>
    <dc:date>2017-03-09T17:36:00Z</dc:date>
    <item>
      <title>Merge</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge/m-p/339711#M77526</link>
      <description>&lt;PRE&gt;Dear All:
These are my two data sets

ID    Time          Value
A     9:30:01       16
A     9:30:02       17
A     9:30:04       18
A     9:30:06       18
B     9:30:01        5
B     9:30:03        6
B     9:30:06        7


I have a time table

Time
9:30:01
9:30:02
9:30:03
9:30:04
9:30:05
9:30:06

and so forth

What I want is

ID    Time          Value
A     9:30:01       16
A     9:30:02       17
A     9:30:03       17
A     9:30:04       18
A     9:30:05       18
A     9:30:06       18
B     9:30:01        5
B     9:30:02        5
B     9:30:03        6
B     9:30:04        6
B     9:30:05        6
B     9:30:06        7&lt;/PRE&gt;&lt;P&gt;Can you please help me.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Mar 2017 17:20:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge/m-p/339711#M77526</guid>
      <dc:creator>RandyStan</dc:creator>
      <dc:date>2017-03-09T17:20:59Z</dc:date>
    </item>
    <item>
      <title>Re: Merge</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge/m-p/339716#M77529</link>
      <description>Just been curious. In real world what are you trying to achieve with this ?</description>
      <pubDate>Thu, 09 Mar 2017 17:36:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge/m-p/339716#M77529</guid>
      <dc:creator>anoopmohandas7</dc:creator>
      <dc:date>2017-03-09T17:36:00Z</dc:date>
    </item>
    <item>
      <title>Re: Merge</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge/m-p/339720#M77532</link>
      <description>&lt;P&gt;I assume&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Your time table is intended to represent every exact second between 09:30:01 and 09:30:06.&amp;nbsp;It has no holes&lt;/LI&gt;
&lt;LI&gt;You want last observation carried forwad for missing time points&lt;/LI&gt;
&lt;LI&gt;Every ID starts with a non-missing record at time=09:30:01&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
  input ID :$1.   Time :time8.0         Value;
  format time time8.0;
datalines;
A     9:30:01       16
A     9:30:02       17
A     9:30:04       18
A     9:30:06       18
B     9:30:01        5
B     9:30:03        6
B     9:30:06        7
run;
data time_table;
  input time time8.0;
  format time time8.0;
datalines;
9:30:01
9:30:02
9:30:03
9:30:04
9:30:05
9:30:06
run;

data want (keep=id time value);
  if _n_=1 then set time_table (rename=(time=tim_end)) nobs=ntimes point=ntimes;

  set one;
  by id;
  merge one one (firstobs=2 keep=time rename=(time=nxt_tim));

  if last.id then nxt_tim=tim_end+1;
  do time=time to nxt_tim-1; output; end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 09 Mar 2017 17:49:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge/m-p/339720#M77532</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-03-09T17:49:58Z</dc:date>
    </item>
    <item>
      <title>Re: Merge</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge/m-p/339950#M77621</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
  input ID :$1.   Time :time8.0         Value;
  format time time8.0;
datalines;
A     9:30:01       16
A     9:30:02       17
A     9:30:04       18
A     9:30:06       18
B     9:30:01        5
B     9:30:03        6
B     9:30:06        7
run;
data time_table;
  input time time8.0;
  format time time8.0;
datalines;
9:30:01
9:30:02
9:30:03
9:30:04
9:30:05
9:30:06
run;

proc sql;
create table temp as
select a.*,b.value
from (
select * from
(select distinct id from one),(select distinct time from time_table) 
) as a
left join one as b 
on a.id=b.id and a.time=b.time;
quit;

data want;
 set temp;
 by id;
 retain v;
 if first.id then v=.;
 if not missing(value) then v=value;
 drop value;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 10 Mar 2017 09:50:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge/m-p/339950#M77621</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-03-10T09:50:28Z</dc:date>
    </item>
    <item>
      <title>Re: Merge</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge/m-p/340008#M77653</link>
      <description>&lt;P&gt;Note that if you want to fill each second between consecutive times, you don't need&lt;/P&gt;
&lt;P&gt;a time table. Using &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt; input dataset "one", this should give the wanted result :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* We sort by descending time because there is no "lead" operator in SAS */&lt;BR /&gt;proc sort data=one out=eno;
by ID descending time;
run;

data eno_filled;
set eno(rename=(time=oldTime));
by ID;
format time time8.0;
drop oldTime;

time=lag(oldTime)-1;

if first.ID then do;
	time=oldTime;
	output;
end;
else do;
	do while (time ge oldTime);			
	    output;
	    time=time-1;
	end;
end;
run;

proc sort data=eno_filled out=want;
by ID time;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 10 Mar 2017 14:38:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge/m-p/340008#M77653</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2017-03-10T14:38:06Z</dc:date>
    </item>
    <item>
      <title>Re: Merge</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge/m-p/340018#M77659</link>
      <description>&lt;P&gt;True, SAS provides no lead&amp;nbsp;function.&amp;nbsp; After all it can't&amp;nbsp;be a simple "lookahead" like in Excel.&amp;nbsp; But then&amp;nbsp; lag is not a "lookback", it is a queue manager, which is a big difference when dealing with by groups, or interleaved series.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But the absence of a lead function is usually not enough of a reason to sort by descending time, use the lag function, and then re-sort to ascending order - a big penaly for large data sets.&amp;nbsp; It's usually far more efficient&amp;nbsp;to take advantage of the "firstobs=" parameter for data set names as objects of the SET or MERGE statements.&amp;nbsp; That's why I submitted the code with merge.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Implementing a lead function could not be analogous to the lag function.&amp;nbsp; It' would not be too burdomsome&amp;nbsp;for "normal" leads (constant lead length and no filtering of upcoming observations), but how would you be able to deal with the following?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; data want;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set sashelp.class;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if sex='F' then last_age=lag(age);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else if sex='M' then last_age=lag(age);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Because lag is a queue manager, there are &lt;EM&gt;&lt;STRONG&gt;two&lt;/STRONG&gt;&lt;/EM&gt; queues being maintained above, one for each sex.&amp;nbsp; Note that there is no extra disk input processing required.&amp;nbsp; Now imagine&amp;nbsp;there were such a thing as a lead function, i.e.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; next_age=lead(age)&lt;/P&gt;
&lt;P&gt;You still would not have a way to maintain two separate series of leads, because the lead function would provide sas with no guidance on how to filter the upcoming observations.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;lt;promotion&amp;gt;&lt;/P&gt;
&lt;P&gt;If you are coming to SGF next month, these are some of the issues I'll be presenting (&lt;EM&gt;&lt;STRONG&gt;Leads and Lags: Static and Dynamic Queues in the SAS® DATA STEP&lt;/STRONG&gt;&lt;/EM&gt;) Tuesday, April 4 5pm-6pm.&lt;/P&gt;
&lt;P&gt;&amp;lt;/promotion&amp;gt;&lt;/P&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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Mar 2017 18:27:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge/m-p/340018#M77659</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-03-10T18:27:55Z</dc:date>
    </item>
    <item>
      <title>Re: Merge</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge/m-p/340349#M77772</link>
      <description>&lt;P&gt;Thank you so much.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Mar 2017 09:54:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge/m-p/340349#M77772</guid>
      <dc:creator>RandyStan</dc:creator>
      <dc:date>2017-03-13T09:54:38Z</dc:date>
    </item>
    <item>
      <title>Re: Merge</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge/m-p/340363#M77774</link>
      <description>&lt;P&gt;Thanks &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt; for your interesting comments.&lt;/P&gt;
&lt;P&gt;My goal was to show that the time table was not necessary because it merely increments a counter.&lt;/P&gt;
&lt;P&gt;Using a merge as you suggest, the program becomes :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set one;
	by ID;
	merge one one (firstobs=2 keep=time rename=(time=nextTime));
	drop nextTime;

	if last.ID then do;
		output;
	end;
	else do;
		do while (time lt nextTime);			
		    output;
		    time=time+1;
		end;
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 13 Mar 2017 10:53:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge/m-p/340363#M77774</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2017-03-13T10:53:52Z</dc:date>
    </item>
  </channel>
</rss>

