<?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: Find pairs of a single variable from first and last in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Find-pairs-of-a-single-variable-from-first-and-last/m-p/787079#M251410</link>
    <description>&lt;P&gt;Have you seen my sample code that I posted a while ago at&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Programming/Find-pairs-of-a-single-variable-from-first-and-last/m-p/783388#M249772" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/Find-pairs-of-a-single-variable-from-first-and-last/m-p/783388#M249772&lt;/A&gt;&amp;nbsp;?&lt;/P&gt;
&lt;P&gt;Isn't it what you are looking for?&lt;/P&gt;
&lt;P&gt;The only thing "wrong" with my code is that you need to increase the "vals" array size (in my example I set to "10") to some big number that will never be reached of events per userid&lt;/P&gt;</description>
    <pubDate>Wed, 22 Dec 2021 10:57:50 GMT</pubDate>
    <dc:creator>EyalGonen</dc:creator>
    <dc:date>2021-12-22T10:57:50Z</dc:date>
    <item>
      <title>Find pairs of a single variable from first and last</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-pairs-of-a-single-variable-from-first-and-last/m-p/783384#M249769</link>
      <description>&lt;P&gt;Hi guys,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Need some help with this please!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If I have the following data&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;User ID&lt;/TD&gt;&lt;TD&gt;Dates&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;23576&lt;/TD&gt;&lt;TD&gt;08/10/1998&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;23576&lt;/TD&gt;&lt;TD&gt;08/11/1998&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;23576&lt;/TD&gt;&lt;TD&gt;08/12/2000&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;23576&lt;/TD&gt;&lt;TD&gt;09/10/1998&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;85028&lt;/TD&gt;&lt;TD&gt;07/09/1976&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;85028&lt;/TD&gt;&lt;TD&gt;07/10/1967&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;85028&lt;/TD&gt;&lt;TD&gt;10/07/1967&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;13857&lt;/TD&gt;&lt;TD&gt;19/03/2001&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;13857&lt;/TD&gt;&lt;TD&gt;18/03/2001&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I want is all possible pairs of the dates associated with that user ID.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So for ID&amp;nbsp;23576 I would want table like this:&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;User ID&lt;/TD&gt;&lt;TD&gt;Date A&lt;/TD&gt;&lt;TD&gt;Date B&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;23576&lt;/TD&gt;&lt;TD&gt;08/10/1998&lt;/TD&gt;&lt;TD&gt;08/11/1998&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;23576&lt;/TD&gt;&lt;TD&gt;08/10/1998&lt;/TD&gt;&lt;TD&gt;08/12/2000&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;23576&lt;/TD&gt;&lt;TD&gt;08/10/1998&lt;/TD&gt;&lt;TD&gt;09/10/1998&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;23576&lt;/TD&gt;&lt;TD&gt;08/11/1998&lt;/TD&gt;&lt;TD&gt;08/12/2000&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;23576&lt;/TD&gt;&lt;TD&gt;08/11/1998&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;09/10/1998&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;23576&lt;/TD&gt;&lt;TD&gt;08/12/2000&lt;/TD&gt;&lt;TD&gt;09/10/1998&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope that makes sense.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you so much!&lt;/P&gt;</description>
      <pubDate>Wed, 01 Dec 2021 13:53:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-pairs-of-a-single-variable-from-first-and-last/m-p/783384#M249769</guid>
      <dc:creator>Feefee</dc:creator>
      <dc:date>2021-12-01T13:53:40Z</dc:date>
    </item>
    <item>
      <title>Re: Find pairs of a single variable from first and last</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-pairs-of-a-single-variable-from-first-and-last/m-p/783388#M249772</link>
      <description>&lt;P&gt;See if this helps:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data in;
	userid=1; value=1; output;
	userid=1; value=2; output;
	userid=1; value=3; output;
	userid=2; value=10; output;
	userid=2; value=11; output;
run;

data outds;
	set in;
	by userid;
	array vals(10) _temporary_;
	retain cnt 0;
	if first.userid then do;
		cnt = 0;
	end;
	cnt + 1;
	vals(cnt) = value;
	if last.userid then do;
		do i = 1 to cnt-1;
			do j = i+1 to cnt;
				value1 = vals(i);
				value2 = vals(j);
				output;
				keep userid value1 value2;
			end;
		end;
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 01 Dec 2021 14:38:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-pairs-of-a-single-variable-from-first-and-last/m-p/783388#M249772</guid>
      <dc:creator>EyalGonen</dc:creator>
      <dc:date>2021-12-01T14:38:18Z</dc:date>
    </item>
    <item>
      <title>Re: Find pairs of a single variable from first and last</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-pairs-of-a-single-variable-from-first-and-last/m-p/783389#M249773</link>
      <description>&lt;P&gt;You could also join the table with itself but this would produce more rows than you want (per your example)&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data in;
	userid=1; value=1; output;
	userid=1; value=2; output;
	userid=1; value=3; output;
	userid=2; value=10; output;
	userid=2; value=11; output;
run;

proc sql;
	create table outsql as 
	select a.*, b.value as value2
	from in a, in b
	where a.userid = b.userid and a.value ne b.value
	;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 01 Dec 2021 14:41:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-pairs-of-a-single-variable-from-first-and-last/m-p/783389#M249773</guid>
      <dc:creator>EyalGonen</dc:creator>
      <dc:date>2021-12-01T14:41:08Z</dc:date>
    </item>
    <item>
      <title>Re: Find pairs of a single variable from first and last</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-pairs-of-a-single-variable-from-first-and-last/m-p/783392#M249775</link>
      <description>Nice, although word of warning your array needs to be big enough to hold all the values.&lt;BR /&gt;Change the number of array elements to 2 and this example will error.</description>
      <pubDate>Wed, 01 Dec 2021 14:55:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-pairs-of-a-single-variable-from-first-and-last/m-p/783392#M249775</guid>
      <dc:creator>AMSAS</dc:creator>
      <dc:date>2021-12-01T14:55:19Z</dc:date>
    </item>
    <item>
      <title>Re: Find pairs of a single variable from first and last</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-pairs-of-a-single-variable-from-first-and-last/m-p/783415#M249784</link>
      <description>Hi thanks for the info.&lt;BR /&gt;&lt;BR /&gt;I am getting a java.lang.ArrayIndexoutofboundsexception: 10 error &lt;span class="lia-unicode-emoji" title=":confused_face:"&gt;😕&lt;/span&gt;&lt;BR /&gt;&lt;BR /&gt;How can i fix that please?</description>
      <pubDate>Wed, 01 Dec 2021 16:37:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-pairs-of-a-single-variable-from-first-and-last/m-p/783415#M249784</guid>
      <dc:creator>Feefee</dc:creator>
      <dc:date>2021-12-01T16:37:57Z</dc:date>
    </item>
    <item>
      <title>Re: Find pairs of a single variable from first and last</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-pairs-of-a-single-variable-from-first-and-last/m-p/783573#M249880</link>
      <description>&lt;P&gt;You are correct, the size of the array should be set to the maximum number of rows possible for a single userid. I provided just an example to illustrate the solution.&lt;/P&gt;</description>
      <pubDate>Thu, 02 Dec 2021 09:35:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-pairs-of-a-single-variable-from-first-and-last/m-p/783573#M249880</guid>
      <dc:creator>EyalGonen</dc:creator>
      <dc:date>2021-12-02T09:35:46Z</dc:date>
    </item>
    <item>
      <title>Re: Find pairs of a single variable from first and last</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-pairs-of-a-single-variable-from-first-and-last/m-p/783574#M249881</link>
      <description>&lt;P&gt;SQL is good at building cartesian joins, you just need the correct comparison:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines dlm="09"x;
input User_ID $ Dates:ddmmyy10.;
datalines;
23576	08/10/1998
23576	08/11/1998
23576	08/12/2000
23576	09/10/1998
85028	07/09/1976
85028	07/10/1967
85028	10/07/1967
13857	19/03/2001
13857	18/03/2001
;

proc sql;
create table want as
  select
    a.user_id,
    a.dates as date_a format=yymmdd10.,
    b.dates as date_b format=yymmdd10.
  from have a inner join have b
  on a.user_id = b.user_id and a.dates lt b.dates
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Also note how data is presented in a data step with datalines, this makes it much easier to develop and test code. Please do so in the future.&lt;/P&gt;</description>
      <pubDate>Thu, 02 Dec 2021 09:53:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-pairs-of-a-single-variable-from-first-and-last/m-p/783574#M249881</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-12-02T09:53:42Z</dc:date>
    </item>
    <item>
      <title>Re: Find pairs of a single variable from first and last</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-pairs-of-a-single-variable-from-first-and-last/m-p/786906#M251308</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your response. How would I do this in data steps instead of proc sql please?&lt;/P&gt;</description>
      <pubDate>Tue, 21 Dec 2021 10:29:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-pairs-of-a-single-variable-from-first-and-last/m-p/786906#M251308</guid>
      <dc:creator>Feefee</dc:creator>
      <dc:date>2021-12-21T10:29:40Z</dc:date>
    </item>
    <item>
      <title>Re: Find pairs of a single variable from first and last</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-pairs-of-a-single-variable-from-first-and-last/m-p/786908#M251310</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;Thank you for your response. How would I do this in data steps instead of proc sql please?&lt;BR /&gt;&lt;BR /&gt;I am not using sas directly and proc sql isn't working properly unfortunately,</description>
      <pubDate>Tue, 21 Dec 2021 10:31:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-pairs-of-a-single-variable-from-first-and-last/m-p/786908#M251310</guid>
      <dc:creator>Feefee</dc:creator>
      <dc:date>2021-12-21T10:31:01Z</dc:date>
    </item>
    <item>
      <title>Re: Find pairs of a single variable from first and last</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-pairs-of-a-single-variable-from-first-and-last/m-p/786918#M251314</link>
      <description>&lt;P&gt;For cartesian joins, SQL is the tool of choice. My code is tested and works. Where is your problem?&lt;/P&gt;</description>
      <pubDate>Tue, 21 Dec 2021 11:18:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-pairs-of-a-single-variable-from-first-and-last/m-p/786918#M251314</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-12-21T11:18:58Z</dc:date>
    </item>
    <item>
      <title>Re: Find pairs of a single variable from first and last</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-pairs-of-a-single-variable-from-first-and-last/m-p/786922#M251317</link>
      <description>I'm getting a redefinition of local variable error.&lt;BR /&gt;&lt;BR /&gt;Also, I am not using sas but a similar product and the sql isnt very liked. So I am trying to write it in a datastep instead but struggling. Any help would be appreciated please &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Tue, 21 Dec 2021 12:12:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-pairs-of-a-single-variable-from-first-and-last/m-p/786922#M251317</guid>
      <dc:creator>Feefee</dc:creator>
      <dc:date>2021-12-21T12:12:26Z</dc:date>
    </item>
    <item>
      <title>Re: Find pairs of a single variable from first and last</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-pairs-of-a-single-variable-from-first-and-last/m-p/786975#M251348</link>
      <description>&lt;PRE&gt;data have;
input element$ 1-2 qt;
datalines;
A 1
B 2
Cc3
D 4
Ee5
;

data want;
 set have end=last nobs=nobs;
 array x{999} $ 32 _temporary_;
 x{_n_}=element;
 if last then do;
   do i=1 to nobs-1;
     var1=x{i};
     do j=i+1 to nobs;
       var2=x{j};output;
	 end;
   end;
 end;
 keep var1 var2;
run;&lt;/PRE&gt;&lt;P&gt;I found this, and it works but I want to apply this to each User_ID and not the entire list. I am trying method with first and last but I am getting null pointer exception.&lt;/P&gt;&lt;P&gt;This is what I have so far:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data want;
 set have;
 retain total_rows 0 a 1;
 by User_ID;
 
total_rows = total_rows +1;
a = a +1;

 if first.User_ID then do;
 	a=1;
 	array x{999} $ 32 _temporary_;
	x{_n_}=dates;
end;

if last.User_ID then do;
do i = 1 to total_rows-1;
  dates=x{i};
       do j=rownum+1 to total_rows;
       var2=x{j};
       output;
	 end;
   end;
end;
run;&lt;/PRE&gt;&lt;P&gt;It works find until the do = i bit..&lt;/P&gt;&lt;P&gt;I have formatted etc its just the last bit I am struggling to get &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Dec 2021 16:38:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-pairs-of-a-single-variable-from-first-and-last/m-p/786975#M251348</guid>
      <dc:creator>Feefee</dc:creator>
      <dc:date>2021-12-21T16:38:15Z</dc:date>
    </item>
    <item>
      <title>Re: Find pairs of a single variable from first and last</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-pairs-of-a-single-variable-from-first-and-last/m-p/787079#M251410</link>
      <description>&lt;P&gt;Have you seen my sample code that I posted a while ago at&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Programming/Find-pairs-of-a-single-variable-from-first-and-last/m-p/783388#M249772" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/Find-pairs-of-a-single-variable-from-first-and-last/m-p/783388#M249772&lt;/A&gt;&amp;nbsp;?&lt;/P&gt;
&lt;P&gt;Isn't it what you are looking for?&lt;/P&gt;
&lt;P&gt;The only thing "wrong" with my code is that you need to increase the "vals" array size (in my example I set to "10") to some big number that will never be reached of events per userid&lt;/P&gt;</description>
      <pubDate>Wed, 22 Dec 2021 10:57:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-pairs-of-a-single-variable-from-first-and-last/m-p/787079#M251410</guid>
      <dc:creator>EyalGonen</dc:creator>
      <dc:date>2021-12-22T10:57:50Z</dc:date>
    </item>
    <item>
      <title>Re: Find pairs of a single variable from first and last</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-pairs-of-a-single-variable-from-first-and-last/m-p/787101#M251419</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/373950"&gt;@Feefee&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;I'm getting a redefinition of local variable error.&lt;BR /&gt;&lt;BR /&gt;Also, I am not using sas but a similar product and the sql isnt very liked. So I am trying to write it in a datastep instead but struggling. Any help would be appreciated please &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Buying cheap means buying double, says my grandma &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One modern data step solution is the use of a hash object, which grows as needed (using the same "have" I used earlier):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want2;
if 0 then set have;
format date_a date_b yymmdd10.;
if _n_ = 1
then do;
  declare hash h (multidata:"yes");
  declare hiter hi ("h");
  h.definekey("user_id");
  h.definedata("user_id","dates");
  h.definedone();
end;
do until (last.user_id);
  set have;
  by user_id notsorted;
  rc = h.add();
end;
do until (last.user_id);
  set have (rename=(dates=date_a));
  by user_id notsorted;
  rc = hi.first();
  do while (rc = 0);
    date_b = dates;
    if date_a &amp;lt; date_b then output;
    rc = hi.next();
  end;
end;
rc = h.clear();
keep user_id date_a date_b;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 Dec 2021 13:53:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-pairs-of-a-single-variable-from-first-and-last/m-p/787101#M251419</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-12-22T13:53:18Z</dc:date>
    </item>
  </channel>
</rss>

