<?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: Assign values from one file to the other dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Assign-values-from-one-file-to-the-other-dataset/m-p/712309#M219600</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

data master;
input Field_One $ 1-12 Field_Two $ 14;
datalines;
Lancelot     Y 
Sehlola      N 
Johannesburg N 
Pretoria     Y 
Randburg     N 
;
 
data num;
input Field_One $;
datalines;
1
2
3
4
5
;

data want(drop = n k rc);

   if _N_ = 1 then do;
      dcl hash h();
      h.definekey("k");
      h.definedata("Derived_Field");
      h.definedone();

      do k = 1 by 1 until (z1);
         set num(rename=Field_One=Derived_Field) end = z1;
         h.add();
      end;
   end;

   set master;

   if Field_Two = "Y" then Derived_Field = "Existing Value";
   else do;
      n + 1;
      rc = h.find(key : n);
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 19 Jan 2021 05:32:26 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2021-01-19T05:32:26Z</dc:date>
    <item>
      <title>Assign values from one file to the other dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-values-from-one-file-to-the-other-dataset/m-p/712307#M219598</link>
      <description>&lt;P&gt;Hi All&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have two datasets. is a master file as seen below. The other file holds numerical data in ascending order which must be assigned to the master file depending the following condition:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Mater Data&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;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;Else&amp;nbsp;"Field_Two" = "N" then&amp;nbsp;"Derived_Field" = "Field_One" - of the numerical dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Once you have used the first record of the numerical dataset, it cannot be reused in the master file.&lt;/P&gt;</description>
      <pubDate>Tue, 19 Jan 2021 05:06:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-values-from-one-file-to-the-other-dataset/m-p/712307#M219598</guid>
      <dc:creator>lsehlola</dc:creator>
      <dc:date>2021-01-19T05:06:14Z</dc:date>
    </item>
    <item>
      <title>Re: Assign values from one file to the other dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-values-from-one-file-to-the-other-dataset/m-p/712309#M219600</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

data master;
input Field_One $ 1-12 Field_Two $ 14;
datalines;
Lancelot     Y 
Sehlola      N 
Johannesburg N 
Pretoria     Y 
Randburg     N 
;
 
data num;
input Field_One $;
datalines;
1
2
3
4
5
;

data want(drop = n k rc);

   if _N_ = 1 then do;
      dcl hash h();
      h.definekey("k");
      h.definedata("Derived_Field");
      h.definedone();

      do k = 1 by 1 until (z1);
         set num(rename=Field_One=Derived_Field) end = z1;
         h.add();
      end;
   end;

   set master;

   if Field_Two = "Y" then Derived_Field = "Existing Value";
   else do;
      n + 1;
      rc = h.find(key : n);
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 19 Jan 2021 05:32:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-values-from-one-file-to-the-other-dataset/m-p/712309#M219600</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-01-19T05:32:26Z</dc:date>
    </item>
    <item>
      <title>Re: Assign values from one file to the other dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-values-from-one-file-to-the-other-dataset/m-p/712311#M219602</link>
      <description>&lt;P&gt;Thank you so much. Works like a charm. Do you mind explaining the logic?&lt;/P&gt;</description>
      <pubDate>Tue, 19 Jan 2021 05:42:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-values-from-one-file-to-the-other-dataset/m-p/712311#M219602</guid>
      <dc:creator>lsehlola</dc:creator>
      <dc:date>2021-01-19T05:42:00Z</dc:date>
    </item>
    <item>
      <title>Re: Assign values from one file to the other dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-values-from-one-file-to-the-other-dataset/m-p/712319#M219606</link>
      <description>&lt;P&gt;Sure. In the first iteration of the data step, I declare a hash table h. I fill it with data from &lt;STRONG&gt;num&lt;/STRONG&gt; using an ascending key k.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then I read the &lt;STRONG&gt;master&lt;/STRONG&gt; data using the Set Statement. If Field_Two = "Y" Then we simply set Derived_Field to "Existing Value".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If not, we increment n by one and use that value to lookup Field_Two in num. That way, we will never lookup using the same key twice.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this makes sense &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Jan 2021 06:35:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-values-from-one-file-to-the-other-dataset/m-p/712319#M219606</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-01-19T06:35:41Z</dc:date>
    </item>
    <item>
      <title>Re: Assign values from one file to the other dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-values-from-one-file-to-the-other-dataset/m-p/712378#M219629</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data master;
input Field_One $ 1-12 Field_Two $ 14;
datalines;
Lancelot     Y 
Sehlola      N 
Johannesburg N 
Pretoria     Y 
Randburg     N 
;
 
data num;
input Field_One $;
id+1;
datalines;
1
2
3
4
5
;

data want;
   if _n_ = 1 then do;
      if 0 then set num(rename=(Field_One=F));
      dcl hash h(dataset:'num(rename=(Field_One=F))',ordered:'y');
	  dcl hiter hi('h');
      h.definekey('id');
      h.definedata('id','F');
      h.definedone();
   end;
set master;
length Derived_Field $ 40;
if Field_Two='Y' then Derived_Field='Existing Value';
if Field_Two='N' then do;
 hi.first();
 Derived_Field=F ;_id=id;
 hi.next();
 h.remove(key:_id);
end;
drop F _id id;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 19 Jan 2021 11:19:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-values-from-one-file-to-the-other-dataset/m-p/712378#M219629</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-01-19T11:19:37Z</dc:date>
    </item>
  </channel>
</rss>

