<?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: Use of RETAIN to grab informations in other observations in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Use-of-RETAIN-to-grab-informations-in-other-observations/m-p/599417#M173033</link>
    <description>&lt;P&gt;Both solutions provided by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/137664"&gt;@jdwaterman91&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;works. A big thank to you!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, let says that I have 5-6 columns linked to a policy number that I want to add to the left of the coverage line, is there a way to do it? Of course, those columns exists in the dataset, but for the insured part (coverage), thoses data are set to null...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 25 Oct 2019 17:26:48 GMT</pubDate>
    <dc:creator>jpprovost</dc:creator>
    <dc:date>2019-10-25T17:26:48Z</dc:date>
    <item>
      <title>Use of RETAIN to grab informations in other observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-of-RETAIN-to-grab-informations-in-other-observations/m-p/599363#M173006</link>
      <description>&lt;P&gt;Hi folks,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a dataset that contains 3 types of observations, let says Payor (P), Insured (I) and Payor-Insured (PI).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've made a simple dataset with the following code for demonstration purpose.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test_retain;
infile datalines;
input
no_police 3.
rel_police $1.
no_cov 3.
rel_cov $1.
;
datalines;
110P
222P
    110I
    465I
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The first two lines are the payor of the policy #110 and #222. They are not insured, so they have missing values.&lt;/P&gt;
&lt;P&gt;However, the insured of those two policies are the 3rd and 4th rows. They are not the payor, so they have missing values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I would like to do is to use the RETAIN function (I'm new with this one and I struggle a lot to do what I want to achieve) to scan the policy # (no_police) and to grab it back into the coverages line.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;By example, the 3rd line is the insured of the 1st line which is the policy owner. Is there a way to use the RETAIN function to "scan" the policy number and when no_police = no_cov, then copy that no_police in the line of the insured ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've tried this but obvisouly I really don't know what I'm doing -.-.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test_retain2;
set test_retain;
retain no_police;
if first.no_police = no_cov then no_cov = first.no_police;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any help would be very useful. Thanks in advance. The desired result should look like this:&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;110&amp;nbsp; P&amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; .&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;222&amp;nbsp; P&amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; .&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;110&amp;nbsp; .&amp;nbsp; &amp;nbsp;110&amp;nbsp; I
&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;.&amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp;465&amp;nbsp; I&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Oct 2019 15:30:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-of-RETAIN-to-grab-informations-in-other-observations/m-p/599363#M173006</guid>
      <dc:creator>jpprovost</dc:creator>
      <dc:date>2019-10-25T15:30:31Z</dc:date>
    </item>
    <item>
      <title>Re: Use of RETAIN to grab informations in other observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-of-RETAIN-to-grab-informations-in-other-observations/m-p/599375#M173015</link>
      <description>&lt;P&gt;It will help to show an example of the output should look like for your given input. Especially given that you don't have a match for the NO_COV value in the 4th line of data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I suspect that RETAIN is not what you want as that depends on the order of the data to be useful and I suspect that you cannot be sure of any given order.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Maybe something like:&lt;/P&gt;
&lt;PRE&gt;proc sql;
   create table work.want as
   select a.no_police,a.rel_police, b.no_cov,b.rel_cov
   from (select * from work.test_retain where not missing (no_police) ) as a
        full outer join
        (select * from work.test_retain where not missing (no_cov) ) as b
        on a.no_police=b.no_cov
   ;
quit;&lt;/PRE&gt;
&lt;P&gt;but if there are other variables involved you need to provide some example and the rules for which goes where.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And consider if you have multiple matching occurences of No_police and No_cov&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(is No_police actually supposed to be No_Policy???)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Oct 2019 15:31:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-of-RETAIN-to-grab-informations-in-other-observations/m-p/599375#M173015</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-10-25T15:31:32Z</dc:date>
    </item>
    <item>
      <title>Re: Use of RETAIN to grab informations in other observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-of-RETAIN-to-grab-informations-in-other-observations/m-p/599376#M173016</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;It will help to show an example of the output should look like for your given input. Especially given that you don't have a match for the NO_COV value in the 4th line of data.&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I edited the post while you were typing your answer, my bad.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;For no_police it is in fact no_policy, my French took over my English &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Oct 2019 15:34:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-of-RETAIN-to-grab-informations-in-other-observations/m-p/599376#M173016</guid>
      <dc:creator>jpprovost</dc:creator>
      <dc:date>2019-10-25T15:34:24Z</dc:date>
    </item>
    <item>
      <title>Re: Use of RETAIN to grab informations in other observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-of-RETAIN-to-grab-informations-in-other-observations/m-p/599384#M173020</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test_retain;
infile datalines;
input
no_police 3.
rel_police $1.
no_cov 3.
rel_cov $1.
;
datalines;
110P
222P
    110I
    465I
;
run;

data want;
 if _n_=1 then do;
 dcl hash H  () ;
 H.definekey ("no_police") ;
 H.definedata("no_police") ;
 H.definedone() ;
 do until(z);
  set test_retain(keep=no_police rel_police) end=z;
  if no_police and rel_police='P' then rc=h.add();
 end;
 end;
 z=0;
 set test_retain;
 if rel_cov='I' and missing(no_police) then 
 if h.check(key:no_cov)=0 then no_police=no_cov;
 drop rc;
 run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 25 Oct 2019 15:51:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-of-RETAIN-to-grab-informations-in-other-observations/m-p/599384#M173020</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-10-25T15:51:39Z</dc:date>
    </item>
    <item>
      <title>Re: Use of RETAIN to grab informations in other observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-of-RETAIN-to-grab-informations-in-other-observations/m-p/599386#M173021</link>
      <description>&lt;P&gt;Depending on what your ultimate goal is, it may be better to combine the data into a single rows where the no_police value equals the no_cov value (This also is dependent on whether you have rows with the same values in either of these columns).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, to reach the output specified in your post, you can always create a macro variable to hold your no_police values and then check your no_cov variable against that.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Proc SQL Noprint;
Select Distinct No_Police Into :Check Separated By ','
From Test_Retain
Where No_Police &amp;gt; 0;
Quit;

Data Want;
Set Test_Retain;

If No_Cov In (&amp;amp;Check.) Then No_Police = No_Cov;

Run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 25 Oct 2019 16:04:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-of-RETAIN-to-grab-informations-in-other-observations/m-p/599386#M173021</guid>
      <dc:creator>jdwaterman91</dc:creator>
      <dc:date>2019-10-25T16:04:49Z</dc:date>
    </item>
    <item>
      <title>Re: Use of RETAIN to grab informations in other observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-of-RETAIN-to-grab-informations-in-other-observations/m-p/599417#M173033</link>
      <description>&lt;P&gt;Both solutions provided by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/137664"&gt;@jdwaterman91&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;works. A big thank to you!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, let says that I have 5-6 columns linked to a policy number that I want to add to the left of the coverage line, is there a way to do it? Of course, those columns exists in the dataset, but for the insured part (coverage), thoses data are set to null...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Oct 2019 17:26:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-of-RETAIN-to-grab-informations-in-other-observations/m-p/599417#M173033</guid>
      <dc:creator>jpprovost</dc:creator>
      <dc:date>2019-10-25T17:26:48Z</dc:date>
    </item>
  </channel>
</rss>

