<?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 create a variable based on the matching values of other two variables in the same dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-variable-based-on-the-matching-values-of-other/m-p/480497#M124197</link>
    <description>&lt;P&gt;I think&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;s solution is probably the most efficient you will find if the data fits in memory.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is an alternative data step approach without the hash object.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.have;
   length id partnerId $ 15 yearsEdu 8;
   input id partnerId yearsEdu;
   datalines;
AT-000327-01 AT-000327-02 15 
AT-000327-02 AT-000327-01 20 
AT-000328-01 . 10 
AT-000329-01 AT-000329-02 8 
AT-000329-02 AT-000329-01 13 
;
run;

data want(drop=compPartnerID);
   set have nobs=nobs;
   do _N_=1 to nobs;
      set have(rename=(partnerId=compPartnerID yearsEdu=yearsEdu_P)) point=_N_;
      if id=partnerId then output;
   end;
   if missing(partnerId) then do; 
      call missing(yearsEdu_P);
      output;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 23 Jul 2018 15:43:15 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2018-07-23T15:43:15Z</dc:date>
    <item>
      <title>How to create a variable based on the matching values of other two variables in the same dataset?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-variable-based-on-the-matching-values-of-other/m-p/480384#M124154</link>
      <description>&lt;P&gt;Hello everyone!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm using SAS Studio.&lt;/P&gt;&lt;P&gt;I have the following&amp;nbsp;dataset with three&amp;nbsp;variables: ID is the identification of the respondent, PARTNERID is the identification of the partner of the respondent and YEARSEDU is the number of years of education of the respondent.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;PARTNERID &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; YEARSEDU &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;AT-000327-01 &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;AT-000327-02 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 15 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;AT-000327-02 &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;AT-000327-01 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 20 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;AT-000328-01 &amp;nbsp; &amp;nbsp; &amp;nbsp; . &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;10 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;AT-000329-01 &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;AT-000329-02 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 8 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;AT-000329-02 &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;AT-000329-01 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 13 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I'd like to create another&amp;nbsp;variable that&amp;nbsp;represents the years of education of the partner of the respondent so that I can obtain the following dataset:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;PARTNERID &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; YEARSEDU &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;YEARSEDU_P &amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;AT-000327-01 &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;AT-000327-02 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 15 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;20&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;AT-000327-02 &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;AT-000327-01 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 20 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;15&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;AT-000328-01 &amp;nbsp; &amp;nbsp; &amp;nbsp; . &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;10 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;AT-000329-01 &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;AT-000329-02 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 8 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;13&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;AT-000329-02 &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;AT-000329-01 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 13 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;8&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I know I should compare the value&amp;nbsp;of ID with every value of PARTNERID and if I find a&amp;nbsp;match use the value of YEARSEDU in the matched PARTNERID row as value for YEARSEDU_P in the ID row.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I really have no idea on how to do that, c&lt;/SPAN&gt;&lt;SPAN&gt;an you please&amp;nbsp;help me?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many thanks.&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Sara&lt;/P&gt;</description>
      <pubDate>Mon, 23 Jul 2018 10:27:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-variable-based-on-the-matching-values-of-other/m-p/480384#M124154</guid>
      <dc:creator>saramanzella</dc:creator>
      <dc:date>2018-07-23T10:27:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a variable based on the matching values of other two variables in the same dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-variable-based-on-the-matching-values-of-other/m-p/480397#M124158</link>
      <description>&lt;P&gt;This may not be the most efficient method (and not tested as no test data in datastep):&lt;/P&gt;
&lt;PRE&gt;proc sql;
  create table want as 
  select  a.*,
          b.yearsedu_p
  from    have a
  left join (select id,sum(yearsedu) as yearsedu_p from have group by id) b
  on      a.partnerid=b.id;
quit;&lt;/PRE&gt;</description>
      <pubDate>Mon, 23 Jul 2018 11:58:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-variable-based-on-the-matching-values-of-other/m-p/480397#M124158</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-07-23T11:58:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a variable based on the matching values of other two variables in the same dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-variable-based-on-the-matching-values-of-other/m-p/480412#M124164</link>
      <description>&lt;P&gt;If the dataset is not to large, using a hash-object can solve the problem:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.have;
   length id partnerId $ 15 yearsEdu 8;
   input id partnerId yearsEdu;
   datalines;
AT-000327-01 AT-000327-02 15 
AT-000327-02 AT-000327-01 20 
AT-000328-01 . 10 
AT-000329-01 AT-000329-02 8 
AT-000329-02 AT-000329-01 13 
;
run;

data want;   
   set work.have;

   length yearsEdu_P 8;

   if _n_ = 1 then do;
      declare hash h(dataset: 'work.have(keep=partnerId yearsEdu rename=(yearsEdu = yearsEdu_P))');
      h.defineKey('partnerId');
      h.defineData('yearsEdu_P');
      h.defineDone();
   end;

   if h.find(key: id) ^= 0 then do;
      yearsEdu_P = .;
   end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 23 Jul 2018 13:11:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-variable-based-on-the-matching-values-of-other/m-p/480412#M124164</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2018-07-23T13:11:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a variable based on the matching values of other two variables in the same dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-variable-based-on-the-matching-values-of-other/m-p/480497#M124197</link>
      <description>&lt;P&gt;I think&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;s solution is probably the most efficient you will find if the data fits in memory.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is an alternative data step approach without the hash object.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.have;
   length id partnerId $ 15 yearsEdu 8;
   input id partnerId yearsEdu;
   datalines;
AT-000327-01 AT-000327-02 15 
AT-000327-02 AT-000327-01 20 
AT-000328-01 . 10 
AT-000329-01 AT-000329-02 8 
AT-000329-02 AT-000329-01 13 
;
run;

data want(drop=compPartnerID);
   set have nobs=nobs;
   do _N_=1 to nobs;
      set have(rename=(partnerId=compPartnerID yearsEdu=yearsEdu_P)) point=_N_;
      if id=partnerId then output;
   end;
   if missing(partnerId) then do; 
      call missing(yearsEdu_P);
      output;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 23 Jul 2018 15:43:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-variable-based-on-the-matching-values-of-other/m-p/480497#M124197</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2018-07-23T15:43:15Z</dc:date>
    </item>
  </channel>
</rss>

