<?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 from three other variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-variable-from-three-other-variables/m-p/358665#M274392</link>
    <description>&lt;P&gt;Another easy solution:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; have;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; input ID_I ID_F ID_M;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cards;&lt;/P&gt;&lt;P&gt;11&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 21&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 31&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;12&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 22&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 32&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;13&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 23&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 33&amp;nbsp;&lt;/P&gt;&lt;P&gt;14&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 11&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 12&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;15&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 11&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 13&lt;/P&gt;&lt;P&gt;16&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 11&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 12&lt;/P&gt;&lt;P&gt;17&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 14&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 12&lt;/P&gt;&lt;P&gt;18&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 14&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 15&lt;/P&gt;&lt;P&gt;19&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 14&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 16&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;run&lt;/STRONG&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;&lt;STRONG&gt;data&lt;/STRONG&gt; want;&lt;/P&gt;&lt;P&gt;if _N_ = &lt;STRONG&gt;1&lt;/STRONG&gt; then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if &lt;STRONG&gt;0&lt;/STRONG&gt; then set have;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; declare hash myhash(dataset:'have(keep=ID_F)',multidata:'YES');&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; myhash.defineKey('ID_F');&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; myhash.defineDone( );&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; declare hash myhash2(dataset:'have(keep=ID_M)',multidata:'YES');&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; myhash2.defineKey('ID_M');&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; myhash2.defineDone( );&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;set have;&lt;/P&gt;&lt;P&gt;if myhash.check(key:ID_I)=&lt;STRONG&gt;0&lt;/STRONG&gt; then Sex=&lt;STRONG&gt;2&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;else if myhash2.check(key:ID_I)=&lt;STRONG&gt;0&lt;/STRONG&gt; then Sex=&lt;STRONG&gt;1&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;else Sex=&lt;STRONG&gt;0&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Naveen Srinivasan&lt;/P&gt;</description>
    <pubDate>Mon, 15 May 2017 07:33:11 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2017-05-15T07:33:11Z</dc:date>
    <item>
      <title>Create a new variable from three other variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-variable-from-three-other-variables/m-p/358574#M274386</link>
      <description>&lt;P&gt;Hej SAS Experts, I hope someone can help me on this problem... I use SAS 9.4&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to create a new variable from other variables in an existing data-set.&lt;/P&gt;&lt;P&gt;In this data-set I have three IDs, the ID of an individual, and its parents (father, mother). The problem is, the data-set gives no information about the sex of my individual, but the individual appears again e.g. as a father. I need to check the sex of my individuals...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If I have an example data-set like this:&lt;/P&gt;&lt;P&gt;ID_I &amp;nbsp; ID_F&amp;nbsp; &amp;nbsp;ID_M&amp;nbsp;&lt;/P&gt;&lt;P&gt;11 &amp;nbsp; &amp;nbsp; &amp;nbsp;21 &amp;nbsp; &amp;nbsp; &amp;nbsp;31 &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;12 &amp;nbsp; &amp;nbsp; &amp;nbsp;22 &amp;nbsp; &amp;nbsp; &amp;nbsp;32 &amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;13 &amp;nbsp; &amp;nbsp; &amp;nbsp;23 &amp;nbsp; &amp;nbsp; &amp;nbsp;33 &amp;nbsp;&lt;/P&gt;&lt;P&gt;14 &amp;nbsp; &amp;nbsp; &amp;nbsp;11 &amp;nbsp; &amp;nbsp; &amp;nbsp;12 &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;15 &amp;nbsp; &amp;nbsp; &amp;nbsp;11 &amp;nbsp; &amp;nbsp; &amp;nbsp;13&lt;/P&gt;&lt;P&gt;16 &amp;nbsp; &amp;nbsp; &amp;nbsp;11 &amp;nbsp; &amp;nbsp; &amp;nbsp;12&lt;/P&gt;&lt;P&gt;17 &amp;nbsp; &amp;nbsp; &amp;nbsp;14 &amp;nbsp; &amp;nbsp; &amp;nbsp;12&lt;/P&gt;&lt;P&gt;18 &amp;nbsp; &amp;nbsp; &amp;nbsp;14 &amp;nbsp; &amp;nbsp; &amp;nbsp;15&lt;/P&gt;&lt;P&gt;19 &amp;nbsp; &amp;nbsp; &amp;nbsp;14 &amp;nbsp; &amp;nbsp; &amp;nbsp;16&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;What I want to achieve with a code is an additional variable (1 for Mother, 2 for Father and 0 for unknown):&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID_I &amp;nbsp; &amp;nbsp;ID_F &amp;nbsp; &amp;nbsp;ID_M &amp;nbsp; &amp;nbsp;Sex&lt;/P&gt;&lt;P&gt;11 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;21 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;31 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;/P&gt;&lt;P&gt;12 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;22 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;32 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;13 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;23 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;33 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;14 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;11 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;12 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;/P&gt;&lt;P&gt;15 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;11 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;13 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;16 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;11 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;12 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;17 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;14 &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; 12 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;18 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;14 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;15 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;19 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;14 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;16 &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Does SAS have any function which can do this?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;My idea was to do it with the IN() Function but then I would need to have arrays not variables? Anyway this was my failed try:&lt;/P&gt;&lt;P&gt;Data want;&lt;/P&gt;&lt;P&gt;Set Data Have;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sex= ID_I in ID_F;&lt;/P&gt;&lt;P&gt;put Sex=2;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sex=ID_I in ID_D;&lt;/P&gt;&lt;P&gt;put Sex=1;&lt;/P&gt;&lt;P&gt;RUN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have more than 10.000 observations for every variable, so I don´t know if it is possible to create arrays from my variables? Is there any other smart solution to this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would appreciate your help a lot, I am still new to SAS and this is just too complicated for me.&lt;/P&gt;&lt;P&gt;Thank you in advance Anni&lt;/P&gt;</description>
      <pubDate>Sun, 14 May 2017 17:59:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-new-variable-from-three-other-variables/m-p/358574#M274386</guid>
      <dc:creator>Anni2017</dc:creator>
      <dc:date>2017-05-14T17:59:58Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new variable from three other variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-variable-from-three-other-variables/m-p/358575#M274387</link>
      <description>&lt;P&gt;10,000 is really small for a SAS array. You can use them for many millions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If I correctly understand what you're trying to do, try the following:&lt;/P&gt;
&lt;PRE&gt;data want (drop=n);&lt;BR /&gt; array fathers(100000) _temporary_;&lt;BR /&gt; array mothers(100000) _temporary_;&lt;BR /&gt; do until (last);&lt;BR /&gt; set have end=last;&lt;BR /&gt; n+1;&lt;BR /&gt; fathers(n)=ID_F;&lt;BR /&gt; mothers(n)=ID_M;&lt;BR /&gt; end;&lt;BR /&gt; do until (last2);&lt;BR /&gt; set have end=last2;&lt;BR /&gt; if id_i in fathers then sex=2;&lt;BR /&gt; else if id_i in mothers then sex=1;&lt;BR /&gt; else sex=0;&lt;BR /&gt; output;&lt;BR /&gt; end;&lt;BR /&gt;run;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 14 May 2017 22:32:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-new-variable-from-three-other-variables/m-p/358575#M274387</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-05-14T22:32:30Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new variable from three other variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-variable-from-three-other-variables/m-p/358586#M274388</link>
      <description>&lt;P&gt;This is a bit of a longer approach, but more introductory level solution that you should be able to modify as necessary.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Create sample data - not required in your actual code. Use the data you already have.&lt;/P&gt;
&lt;P&gt;2. Create master list of gender data from original data by appending the two datasets and assigning F/M as indicated.&lt;/P&gt;
&lt;P&gt;3. Ensure list above has one record per ID and gender - see comments in code below.&lt;/P&gt;
&lt;P&gt;4. Merge results back in and keep only records from the original file.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*input sample data;

data have;
	input ID_I ID_F ID_M;
	cards;
11      21      31     
12      22      32    
13      23      33  
14      11      12      
15      11      13
16      11      12
17      14      12
18      14      15
19      14      16
;
run;

*Get gender from file originally - should make sure this is unique
one record per ID;

data gender;
	set have (rename=ID_F=ID in=Males) have (rename=ID_M=ID in=Females);

	if Males then
		sex=2;

	if Females then
		sex=1;
	keep ID SEX;
run;

*Sort for merging;

proc sort data=gender nodupkey;
	by id sex;
run;

*CHECK FOR duplicates - if the log shows duplicates you have a problem with your data
mainly ID identified as both male and female; 
proc sort data=gender out=checkme nodupkey;
by id;
run;

proc sort data=have;
	by ID_I;
run;


*merge data;

data want;
	merge have (in=original) gender (rename=ID=ID_I);
	by ID_I;

	if original;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 14 May 2017 19:02:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-new-variable-from-three-other-variables/m-p/358586#M274388</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-05-14T19:02:12Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new variable from three other variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-variable-from-three-other-variables/m-p/358600#M274389</link>
      <description>&lt;P&gt;Another solution similar logic to&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;'s, but with a format:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;/* step 1 - create a format */&lt;/P&gt;
&lt;P&gt;data ID_sex(rename=(sex=label));&lt;/P&gt;
&lt;P&gt;&amp;nbsp; set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; retain fmtname 'sexf';&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; start = id_f; sex='2'; output;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; start = id_m; sex='1'; output;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;keep fmtname start sex;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;proc sort data=id_sex out=ID_sex nodupkey; by start; run;&lt;/P&gt;
&lt;P&gt;proc format cntlin=ID_sex; run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; length sex $1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; sex = put(id_i,sexf.);&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 14 May 2017 20:45:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-new-variable-from-three-other-variables/m-p/358600#M274389</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2017-05-14T20:45:55Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new variable from three other variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-variable-from-three-other-variables/m-p/358619#M274390</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13711"&gt;@art297&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The "do until (last);" won't increment _N_, so you need to change the code inside the loop.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Mark&lt;/P&gt;</description>
      <pubDate>Sun, 14 May 2017 22:17:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-new-variable-from-three-other-variables/m-p/358619#M274390</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-05-14T22:17:18Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new variable from three other variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-variable-from-three-other-variables/m-p/358623#M274391</link>
      <description>&lt;P&gt;I revised my code based on&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;'s comment. He was totally correct and here is the new code:&lt;/P&gt;
&lt;PRE&gt;data have;
  input id_I ID_F ID_M;
  cards;
11      21      31     
12      22      32    
13      23      33  
14      11      12      
15      11      13
16      11      12
17      14      12
18      14      15
19      14      16
;

data want (drop=n);
  array fathers(100000) _temporary_;
  array mothers(100000) _temporary_;
  do until (last);
    set have end=last;
    n+1;
    fathers(n)=ID_F;
    mothers(n)=ID_M;
  end;
  do until (last2);
    set have end=last2;
    if id_i in fathers then sex=2;
    else if id_i in mothers then sex=1;
    else sex=0;
    output;
  end;
run;&lt;/PRE&gt;</description>
      <pubDate>Sun, 14 May 2017 22:33:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-new-variable-from-three-other-variables/m-p/358623#M274391</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-05-14T22:33:02Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new variable from three other variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-variable-from-three-other-variables/m-p/358665#M274392</link>
      <description>&lt;P&gt;Another easy solution:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; have;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; input ID_I ID_F ID_M;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cards;&lt;/P&gt;&lt;P&gt;11&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 21&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 31&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;12&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 22&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 32&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;13&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 23&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 33&amp;nbsp;&lt;/P&gt;&lt;P&gt;14&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 11&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 12&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;15&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 11&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 13&lt;/P&gt;&lt;P&gt;16&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 11&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 12&lt;/P&gt;&lt;P&gt;17&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 14&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 12&lt;/P&gt;&lt;P&gt;18&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 14&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 15&lt;/P&gt;&lt;P&gt;19&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 14&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 16&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;run&lt;/STRONG&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;&lt;STRONG&gt;data&lt;/STRONG&gt; want;&lt;/P&gt;&lt;P&gt;if _N_ = &lt;STRONG&gt;1&lt;/STRONG&gt; then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if &lt;STRONG&gt;0&lt;/STRONG&gt; then set have;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; declare hash myhash(dataset:'have(keep=ID_F)',multidata:'YES');&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; myhash.defineKey('ID_F');&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; myhash.defineDone( );&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; declare hash myhash2(dataset:'have(keep=ID_M)',multidata:'YES');&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; myhash2.defineKey('ID_M');&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; myhash2.defineDone( );&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;set have;&lt;/P&gt;&lt;P&gt;if myhash.check(key:ID_I)=&lt;STRONG&gt;0&lt;/STRONG&gt; then Sex=&lt;STRONG&gt;2&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;else if myhash2.check(key:ID_I)=&lt;STRONG&gt;0&lt;/STRONG&gt; then Sex=&lt;STRONG&gt;1&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;else Sex=&lt;STRONG&gt;0&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Naveen Srinivasan&lt;/P&gt;</description>
      <pubDate>Mon, 15 May 2017 07:33:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-new-variable-from-three-other-variables/m-p/358665#M274392</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2017-05-15T07:33:11Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new variable from three other variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-variable-from-three-other-variables/m-p/358705#M274393</link>
      <description>&lt;P&gt;Dear Art, yes that is exactly what I wanted to do.&lt;/P&gt;&lt;P&gt;The results seem to be correct. Thank you very much for your help!!&lt;/P&gt;&lt;P&gt;Kindest regards Anni&lt;/P&gt;</description>
      <pubDate>Mon, 15 May 2017 11:36:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-new-variable-from-three-other-variables/m-p/358705#M274393</guid>
      <dc:creator>Anni2017</dc:creator>
      <dc:date>2017-05-15T11:36:36Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new variable from three other variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-variable-from-three-other-variables/m-p/358707#M274394</link>
      <description>&lt;P&gt;Thank you Mark the code first gave the wrong result, but now it works perfectly!!&lt;/P&gt;&lt;P&gt;Anni&lt;/P&gt;</description>
      <pubDate>Mon, 15 May 2017 11:38:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-new-variable-from-three-other-variables/m-p/358707#M274394</guid>
      <dc:creator>Anni2017</dc:creator>
      <dc:date>2017-05-15T11:38:38Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new variable from three other variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-variable-from-three-other-variables/m-p/358727#M274395</link>
      <description>&lt;PRE&gt;
I should not jump into this hive.
Since I have some time, I would like to introduce SQL solution.



data have;
	input ID_I ID_F ID_M;
	cards;
11      21      31     
12      22      32    
13      23      33  
14      11      12      
15      11      13
16      11      12
17      14      12
18      14      15
19      14      16
;
run;

proc sql;
create table want as
 select *,case when id_i in (select id_f from have) then 2
               when id_i in (select id_m from have) then 1
               else 0 end as sex 
  from have;
quit;


&lt;/PRE&gt;</description>
      <pubDate>Mon, 15 May 2017 12:42:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-new-variable-from-three-other-variables/m-p/358727#M274395</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-05-15T12:42:38Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new variable from three other variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-variable-from-three-other-variables/m-p/358755#M274396</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;: What hive? Alternative and potentially better solutions, in my opinion, are always welcome. And, in this case, using sql subquerries, is far more efficient than the data step/array solution I had suggested.&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/133023"&gt;@Anni2017&lt;/a&gt;: I would change the accepted solution to&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;'s solution. While both work, the SQL approach is far more efficient and will work with much larger data sets.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Anyone who might be interested: The main reason sql out performed the data step approach was that the size declared for the two arrays has a big impact on performance. &amp;nbsp;That can be adjusted by first running a sql step to create macro variables containing the needed array sizes. I'd love to compare the three approaches&amp;nbsp;on an actual large file, but did a pseudo comparison by just expanding the size of the example data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In my example, the revised data step approach out performed the sql approach, and the sql approach out performed the hash approach:&lt;/P&gt;
&lt;PRE&gt;data have;
	input ID_I ID_F ID_M;
	do i=1 to 500000;
	  output;
	end;
	cards;
11      21      31     
12      22      32    
13      23      33  
14      11      12      
15      11      13
16      11      12
17      14      12
18      14      15
19      14      16
;
run;

proc sql noprint;
  select count(distinct id_m),count(distinct id_f)
    into :m_count, :f_count
      from have
  ;
quit;

data want1 (drop=n:);
  array fathers(&amp;amp;f_count) _temporary_;
  array mothers(&amp;amp;m_count) _temporary_;
  do until (last);
    set have end=last;
    if ID_F not in fathers then do;
      nF+1;
      fathers(nF)=ID_F;
    end;
    if ID_M not in mothers then do;
      nM+1;
      mothers(nM)=ID_M;
    end;
  end;
  do until (last2);
    set have end=last2;
    if id_i in fathers then sex=2;
    else if id_i in mothers then sex=1;
    else sex=0;
    output;
  end;
run;

proc sql;
create table want2 as
 select *,case when id_i in (select id_f from have) then 2
               when id_i in (select id_m from have) then 1
               else 0 end as sex 
  from have;
quit;

data want3;
if _N_ = 1 then do;
     if 0 then set have;
     declare hash myhash(dataset:'have(keep=ID_F)',multidata:'YES');
      myhash.defineKey('ID_F');
      myhash.defineDone( );
     declare hash myhash2(dataset:'have(keep=ID_M)',multidata:'YES');
      myhash2.defineKey('ID_M');
      myhash2.defineDone( );
end;
set have;
if myhash.check(key:ID_I)=0 then Sex=2;
else if myhash2.check(key:ID_I)=0 then Sex=1;
else Sex=0;
run;
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 May 2017 15:01:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-new-variable-from-three-other-variables/m-p/358755#M274396</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-05-15T15:01:48Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new variable from three other variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-variable-from-three-other-variables/m-p/358769#M274397</link>
      <description>&lt;P&gt;Thank you &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13711"&gt;@art297&lt;/a&gt; , &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt; for the very valuable insights and extending the knowledge for somebody like me to learn. I can't appreciate enough, not just this post, many of yours. I have shamelessly used your codes as a verbatim to many of my work. Thank you so much indeed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Naveen Srinivasan&lt;/P&gt;</description>
      <pubDate>Mon, 15 May 2017 15:33:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-new-variable-from-three-other-variables/m-p/358769#M274397</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2017-05-15T15:33:50Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new variable from three other variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-variable-from-three-other-variables/m-p/358800#M274398</link>
      <description>&lt;P&gt;Update for anyone who might be interested: In the code, below, I tried to create a somewhat realistic looking fairly large (n=~100000 records) file. Using it, both the SQL and hash approaches were almost equivalent (about 0.1 seconds cpu time), while the datastep/array solution took almost 80 times longer to run (about 8 seconds) than either the SQL or hash approaches:&lt;/P&gt;
&lt;PRE&gt;/* Create some sample data */
  /* SAS macro that duplicates the Excel RANDBETWEEN function */
  %macro RandBetween(min, max);
     (&amp;amp;min + floor((1+&amp;amp;max-&amp;amp;min)*rand("uniform")))
  %mend;

  data Mothers(drop=i rename=ID_F=ID_M) Fathers(drop=i);
    /* initialize random seed to ensure results are reproducible */
    call streaminit(123);
    do i = 1 to 100000;
      ID_F = %RandBetween(1, 50000);
      if mod(ID_F,2) then output Mothers;
      else output fathers;
    end;
  run;

  data mothers;
    set mothers;
    if _n_ le 49447;
  run;

  data have;
    call streaminit(123);
    set mothers mothers;
    set fathers fathers;
    array parents(*) ID_F ID_M;
    do until (ID_I not in parents);
      ID_I=%RandBetween(1, 50000);
    end;
  run;

proc sql noprint;
  select count(distinct id_m),count(distinct id_f)
    into :m_count, :f_count
      from have
  ;
quit;

data want1 (drop=n:);
  array fathers(&amp;amp;f_count) _temporary_;
  array mothers(&amp;amp;m_count) _temporary_;
  do until (last);
    set have end=last;
    if ID_F not in fathers then do;
      nF+1;
      fathers(nF)=ID_F;
    end;
    if ID_M not in mothers then do;
      nM+1;
      mothers(nM)=ID_M;
    end;
  end;
  do until (last2);
    set have end=last2;
    if id_i in fathers then sex=2;
    else if id_i in mothers then sex=1;
    else sex=0;
    output;
  end;
run;

proc sql;
create table want2 as
 select *,case when id_i in (select id_f from have) then 2
               when id_i in (select id_m from have) then 1
               else 0 end as sex 
  from have;
quit;

data want3;
  if _N_ = 1 then do;
     if 0 then set have;
     declare hash myhash(dataset:'have(keep=ID_F)',multidata:'YES');
      myhash.defineKey('ID_F');
      myhash.defineDone( );
     declare hash myhash2(dataset:'have(keep=ID_M)',multidata:'YES');
      myhash2.defineKey('ID_M');
      myhash2.defineDone( );
  end;
  set have;
  if myhash.check(key:ID_I)=0 then Sex=2;
  else if myhash2.check(key:ID_I)=0 then Sex=1;
  else Sex=0;
run;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 May 2017 17:37:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-new-variable-from-three-other-variables/m-p/358800#M274398</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-05-15T17:37:53Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new variable from three other variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-variable-from-three-other-variables/m-p/358983#M274399</link>
      <description>&lt;PRE&gt;
Arthur.T ,
We are not competitor , we are partner .


&lt;/PRE&gt;</description>
      <pubDate>Tue, 16 May 2017 12:12:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-new-variable-from-three-other-variables/m-p/358983#M274399</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-05-16T12:12:12Z</dc:date>
    </item>
  </channel>
</rss>

