<?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: Create a new variable based on other variables in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Create-a-new-variable-based-on-other-variables/m-p/860773#M82527</link>
    <description>Your code worked perfectly. I was just wondering if a proc sort will be necessary in order not to mess up the pairings? Since the group variable is based on observation number?</description>
    <pubDate>Fri, 24 Feb 2023 20:56:19 GMT</pubDate>
    <dc:creator>ANKH1</dc:creator>
    <dc:date>2023-02-24T20:56:19Z</dc:date>
    <item>
      <title>Create a new variable based on other variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-a-new-variable-based-on-other-variables/m-p/860704#M82519</link>
      <description>&lt;P&gt;There is this dataset. Var1 shows the place within a group. Var2 shows the ID to which the member is related to.&lt;/P&gt;&lt;P&gt;site ID var1 var2&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; 11&amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; 12&amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 11&lt;BR /&gt;2&amp;nbsp; &amp;nbsp; 21&amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;BR /&gt;2&amp;nbsp; &amp;nbsp; 22&amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 21&lt;BR /&gt;2&amp;nbsp; &amp;nbsp; 31&amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;BR /&gt;2&amp;nbsp; &amp;nbsp; 33&amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp;31&lt;/P&gt;&lt;P&gt;We need to create a variable that shows that two IDs belong to a group.&lt;/P&gt;&lt;P&gt;site ID var1 var2 group&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; 11&amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; 12&amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 11&amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;BR /&gt;2&amp;nbsp; &amp;nbsp; 21&amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;BR /&gt;2&amp;nbsp; &amp;nbsp; 22&amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 21&amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;BR /&gt;2&amp;nbsp; &amp;nbsp; 31&amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;BR /&gt;2&amp;nbsp; &amp;nbsp; 33&amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp;31&amp;nbsp; &amp;nbsp; &amp;nbsp; 3&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Feb 2023 15:49:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-a-new-variable-based-on-other-variables/m-p/860704#M82519</guid>
      <dc:creator>ANKH1</dc:creator>
      <dc:date>2023-02-24T15:49:12Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new variable based on other variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-a-new-variable-based-on-other-variables/m-p/860724#M82520</link>
      <description>&lt;P&gt;Please explain the logic used here to derive variable GROUP.&lt;/P&gt;</description>
      <pubDate>Fri, 24 Feb 2023 17:46:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-a-new-variable-based-on-other-variables/m-p/860724#M82520</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-02-24T17:46:58Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new variable based on other variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-a-new-variable-based-on-other-variables/m-p/860734#M82521</link>
      <description>&lt;P&gt;group indicates that ID 11 and ID 12 are associated. var2 indicates that ID 12 is related to ID 11 (value 11).&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Feb 2023 18:24:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-a-new-variable-based-on-other-variables/m-p/860734#M82521</guid>
      <dc:creator>ANKH1</dc:creator>
      <dc:date>2023-02-24T18:24:58Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new variable based on other variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-a-new-variable-based-on-other-variables/m-p/860744#M82522</link>
      <description>&lt;P&gt;is VAR1 always 2 in the first record and VAR1 always 1 in the second record? Is VAR2 always present when var1=1 and missing when var1^=1? Are there ever more than 2 records in each GROUP?&lt;/P&gt;</description>
      <pubDate>Fri, 24 Feb 2023 19:01:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-a-new-variable-based-on-other-variables/m-p/860744#M82522</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-02-24T19:01:35Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new variable based on other variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-a-new-variable-based-on-other-variables/m-p/860745#M82523</link>
      <description>Yes to the first two questions. No, only 2 records per group.</description>
      <pubDate>Fri, 24 Feb 2023 19:14:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-a-new-variable-based-on-other-variables/m-p/860745#M82523</guid>
      <dc:creator>ANKH1</dc:creator>
      <dc:date>2023-02-24T19:14:39Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new variable based on other variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-a-new-variable-based-on-other-variables/m-p/860746#M82524</link>
      <description>&lt;P&gt;I still think there is a piece of logic you haven't explained, but based upon what you have said, this works:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set have;
    group=1+floor((_n_-1)/2);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;I contend something hasn't been explained, because the above solution doesn't even look at the value of var1 and var2.&lt;/P&gt;</description>
      <pubDate>Fri, 24 Feb 2023 19:31:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-a-new-variable-based-on-other-variables/m-p/860746#M82524</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-02-24T19:31:05Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new variable based on other variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-a-new-variable-based-on-other-variables/m-p/860750#M82525</link>
      <description>Thanks. We need for ID 11 and ID 12 to belong to the same unique group. ID 12 shows that it is related to ID 11 by showing in var2 that 11 is the ID it is attached to. var1 shows that ID 11 is number 1 in the list and ID 12 is number 2.</description>
      <pubDate>Fri, 24 Feb 2023 19:43:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-a-new-variable-based-on-other-variables/m-p/860750#M82525</guid>
      <dc:creator>ANKH1</dc:creator>
      <dc:date>2023-02-24T19:43:39Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new variable based on other variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-a-new-variable-based-on-other-variables/m-p/860751#M82526</link>
      <description>&lt;P&gt;Yes, I think I understand that, but what is wrong with my solution that doesn't use VAR1 or VAR2? I get the proper values of GROUP.&lt;/P&gt;</description>
      <pubDate>Fri, 24 Feb 2023 19:45:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-a-new-variable-based-on-other-variables/m-p/860751#M82526</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-02-24T19:45:54Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new variable based on other variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-a-new-variable-based-on-other-variables/m-p/860773#M82527</link>
      <description>Your code worked perfectly. I was just wondering if a proc sort will be necessary in order not to mess up the pairings? Since the group variable is based on observation number?</description>
      <pubDate>Fri, 24 Feb 2023 20:56:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-a-new-variable-based-on-other-variables/m-p/860773#M82527</guid>
      <dc:creator>ANKH1</dc:creator>
      <dc:date>2023-02-24T20:56:19Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new variable based on other variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-a-new-variable-based-on-other-variables/m-p/860777#M82528</link>
      <description>&lt;P&gt;What is not sorted properly?&lt;/P&gt;</description>
      <pubDate>Fri, 24 Feb 2023 21:54:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-a-new-variable-based-on-other-variables/m-p/860777#M82528</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-02-24T21:54:06Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new variable based on other variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-a-new-variable-based-on-other-variables/m-p/860834#M82529</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data x;
infile cards expandtabs;
input site ID var1 var2;
from=catx('|',site,id);
to=catx('|',site,var2);
cards;
1    11    2       .
1    12   1      11
2    21   2       .
2    22   1      21
2    31   2       .
2    33   1     31
;



data have;
set x;
if not missing(id) and not missing(var2);
keep from to;
run;



data full;
  set have end=last;
  if _n_ eq 1 then do;
   declare hash h();
    h.definekey('node');
     h.definedata('node');
     h.definedone();
  end;
  output;
  node=from; h.replace();
  from=to; to=node; output;
  node=from; h.replace();
  if last then h.output(dataset:'node');
  drop node;
run;


data want(keep=node household);
declare hash ha(ordered:'a');
declare hiter hi('ha');
ha.definekey('count');
ha.definedata('last');
ha.definedone();
declare hash _ha(hashexp: 16);
_ha.definekey('key');
_ha.definedone();

if 0 then set full;
declare hash from_to(dataset:'full',hashexp:20,multidata:'y');
 from_to.definekey('from');
 from_to.definedata('to');
 from_to.definedone();

if 0 then set node;
declare hash no(dataset:'node');
declare hiter hi_no('no');
 no.definekey('node');
 no.definedata('node');
 no.definedone();
 

do while(hi_no.next()=0);
 household+1; output;
 count=1;
 key=node;_ha.add();
 last=node;ha.add();
 rc=hi.first();
 do while(rc=0);
   from=last;rx=from_to.find();
   do while(rx=0);
     key=to;ry=_ha.check();
      if ry ne 0 then do;
       node=to;output;rr=no.remove(key:node);
       key=to;_ha.add();
       count+1;
       last=to;ha.add();
      end;
      rx=from_to.find_next();
   end;
   rc=hi.next();
end;
ha.clear();_ha.clear();
end;
stop;
run;

proc sql;
create table final_want as
select a.site,a.id,a.var1,a.var2,b.household as group 
 from x as a left join want as b
  on catx('|',a.site,a.id)=b.node
   order by site,id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 25 Feb 2023 12:22:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-a-new-variable-based-on-other-variables/m-p/860834#M82529</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-02-25T12:22:04Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new variable based on other variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-a-new-variable-based-on-other-variables/m-p/861129#M82530</link>
      <description>&lt;P&gt;I was just wondering if there should be prior step but it works great. Thanks!&lt;/P&gt;</description>
      <pubDate>Mon, 27 Feb 2023 16:11:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-a-new-variable-based-on-other-variables/m-p/861129#M82530</guid>
      <dc:creator>ANKH1</dc:creator>
      <dc:date>2023-02-27T16:11:31Z</dc:date>
    </item>
  </channel>
</rss>

