<?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: Fill a column based on condition in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Fill-a-column-based-on-condition/m-p/902002#M43938</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/456742"&gt;@Nilani&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks for the Tip. I am a beginner user. Couldn't understand your code.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;A SAS hash table is a table in memory that you can use to lookup values. One of the advantages using a hash table is that it removes the need to sort your base table for such lookups.&lt;/P&gt;
&lt;P&gt;Learning what SAS hash tables are and how to use them is something I'd recommend you put on your learning list.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below code creates and loads the hash table. The where clause only loads rows where&amp;nbsp;&lt;EM&gt;applicant_type="A" and not missing(email)&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1699413560876.png" style="width: 704px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/89518iF8B665E0289B352E/image-dimensions/704x88?v=v2" width="704" height="88" role="button" title="Patrick_0-1699413560876.png" alt="Patrick_0-1699413560876.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You then can use the hash find() method to lookup values over a key.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_1-1699413769983.png" style="width: 121px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/89519i6F9B362F976B052D/image-dimensions/121x29?v=v2" width="121" height="29" role="button" title="Patrick_1-1699413769983.png" alt="Patrick_1-1699413769983.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;The find() method will use ApplicationNumber from your base table as key for the lookup and if there is a match will return the value(s) as defined as data - here: email.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's pretty similar to using a Format or Informat for such lookup but with a hash table you can use multiple variables for key definition and you can also define multiple variable as data (=lookup multiple variables at once).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 08 Nov 2023 03:27:12 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2023-11-08T03:27:12Z</dc:date>
    <item>
      <title>Fill a column based on condition</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Fill-a-column-based-on-condition/m-p/900973#M43899</link>
      <description>&lt;P&gt;Hi, I have a dataset like following.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data my_data;
&amp;nbsp; &amp;nbsp; input ApplicationNumber&amp;nbsp; id&amp;nbsp; email $&amp;nbsp; applicant_type $;
&amp;nbsp; &amp;nbsp; datalines;
1 10 a@b.com A
1 20&amp;nbsp; D
2 30 b@c.com A
2 40&amp;nbsp; D
2 50&amp;nbsp; D
3 60 d@e.com D
4 70 A

;
run;&lt;/PRE&gt;&lt;DIV&gt;I want to fill missing emails emails. If the applicant_type is "A" and email is missing do nothing. If the applicant type is "D" and missing email then, group by application number and get the email of applicant type "A" to that has missing email of applicant type "D".&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Expected Output&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;1 10 a@b.com A&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;1 20 a@b.com D&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;2 30 b@c.com A&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;2 40 b@c.com D&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;2 50 b@c.com D&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;3 60 d@e.com D&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;4 70&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;A&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Thanks.&lt;/DIV&gt;</description>
      <pubDate>Wed, 01 Nov 2023 01:42:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Fill-a-column-based-on-condition/m-p/900973#M43899</guid>
      <dc:creator>Nilani</dc:creator>
      <dc:date>2023-11-01T01:42:51Z</dc:date>
    </item>
    <item>
      <title>Re: Fill a column based on condition</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Fill-a-column-based-on-condition/m-p/900979#M43900</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for providing a data step for the input data, however as SAS was going to a new line due to missing data, I added an &lt;FONT face="courier new,courier"&gt;infile&lt;/FONT&gt; statement as shown below.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I had trouble understanding the method you wanted to follow, so please also provide a data step showing what output you expect for the given input, and if you need to cover more situations then please add them to your data steps.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines truncover;
  input
    ApplicationNumber
    id
    email $
    applicant_type $
  ;
  
  format  carrier_date Date9.;
  
  datalines;
1 10 a@b.com A
1 20  D
2 30 b@c.com A
2 40  D
2 50  D
3 60 d@e.com D
4 70 A
;
&lt;/CODE&gt;&lt;/PRE&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;Thanks &amp;amp; kind regards,&lt;/P&gt;
&lt;P&gt;Amir.&lt;/P&gt;</description>
      <pubDate>Tue, 31 Oct 2023 22:19:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Fill-a-column-based-on-condition/m-p/900979#M43900</guid>
      <dc:creator>Amir</dc:creator>
      <dc:date>2023-10-31T22:19:23Z</dc:date>
    </item>
    <item>
      <title>Re: Fill a column based on condition</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Fill-a-column-based-on-condition/m-p/900981#M43901</link>
      <description>&lt;P&gt;Please make it a habit to test the code that creates the sample data because this allows us to spend the time answering your question instead of fixing the sample data creation code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here you go:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines dlm=' ' dsd truncover;
  input ApplicationNumber  id  email $  applicant_type $;
  datalines;
1 10 a@b.com A
1 20  D
2 30 b@c.com A
2 40  D
2 50  D
3 60 d@e.com D
4 70  A
;

data want;
  if _n_=1 then
    do;
      dcl hash h1(dataset:'have(where=(applicant_type="A" and not missing(email))');
      h1.defineKey('ApplicationNumber');
      h1.defineData('email');
      h1.defineDone();
    end;

  set have;
  if applicant_type = 'D' and missing(email) then h1.find();
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 31 Oct 2023 22:28:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Fill-a-column-based-on-condition/m-p/900981#M43901</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-10-31T22:28:03Z</dc:date>
    </item>
    <item>
      <title>Re: Fill a column based on condition</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Fill-a-column-based-on-condition/m-p/900982#M43902</link>
      <description>&lt;P&gt;Thanks Amir. I just updated the question&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 31 Oct 2023 22:23:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Fill-a-column-based-on-condition/m-p/900982#M43902</guid>
      <dc:creator>Nilani</dc:creator>
      <dc:date>2023-10-31T22:23:33Z</dc:date>
    </item>
    <item>
      <title>Re: Fill a column based on condition</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Fill-a-column-based-on-condition/m-p/900995#M43903</link>
      <description>&lt;P&gt;Thanks for the responses. The following code worked for me.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;proc sort data=my_data;
by ApplicationNumber applicant_type;
run;

data want;
set my_data;
by ApplicationNumber;
retain filled_email;
if applicant_type = "A" and not missing (email) then
filled_email=email;
else if applicant_type = "D" and missing (email) then
email=filled_email;
output;
run;&lt;/PRE&gt;</description>
      <pubDate>Wed, 01 Nov 2023 01:41:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Fill-a-column-based-on-condition/m-p/900995#M43903</guid>
      <dc:creator>Nilani</dc:creator>
      <dc:date>2023-11-01T01:41:10Z</dc:date>
    </item>
    <item>
      <title>Re: Fill a column based on condition</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Fill-a-column-based-on-condition/m-p/901000#M43904</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/456742"&gt;@Nilani&lt;/a&gt;&amp;nbsp;Using a hash table approach would avoid the need to sort the source table.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Nov 2023 04:45:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Fill-a-column-based-on-condition/m-p/901000#M43904</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-11-01T04:45:13Z</dc:date>
    </item>
    <item>
      <title>Re: Fill a column based on condition</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Fill-a-column-based-on-condition/m-p/901993#M43937</link>
      <description>&lt;P&gt;Thanks for the Tip. I am a beginner user. Couldn't understand your code.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Nov 2023 01:21:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Fill-a-column-based-on-condition/m-p/901993#M43937</guid>
      <dc:creator>Nilani</dc:creator>
      <dc:date>2023-11-08T01:21:03Z</dc:date>
    </item>
    <item>
      <title>Re: Fill a column based on condition</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Fill-a-column-based-on-condition/m-p/902002#M43938</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/456742"&gt;@Nilani&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks for the Tip. I am a beginner user. Couldn't understand your code.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;A SAS hash table is a table in memory that you can use to lookup values. One of the advantages using a hash table is that it removes the need to sort your base table for such lookups.&lt;/P&gt;
&lt;P&gt;Learning what SAS hash tables are and how to use them is something I'd recommend you put on your learning list.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below code creates and loads the hash table. The where clause only loads rows where&amp;nbsp;&lt;EM&gt;applicant_type="A" and not missing(email)&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1699413560876.png" style="width: 704px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/89518iF8B665E0289B352E/image-dimensions/704x88?v=v2" width="704" height="88" role="button" title="Patrick_0-1699413560876.png" alt="Patrick_0-1699413560876.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You then can use the hash find() method to lookup values over a key.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_1-1699413769983.png" style="width: 121px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/89519i6F9B362F976B052D/image-dimensions/121x29?v=v2" width="121" height="29" role="button" title="Patrick_1-1699413769983.png" alt="Patrick_1-1699413769983.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;The find() method will use ApplicationNumber from your base table as key for the lookup and if there is a match will return the value(s) as defined as data - here: email.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's pretty similar to using a Format or Informat for such lookup but with a hash table you can use multiple variables for key definition and you can also define multiple variable as data (=lookup multiple variables at once).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Nov 2023 03:27:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Fill-a-column-based-on-condition/m-p/902002#M43938</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-11-08T03:27:12Z</dc:date>
    </item>
  </channel>
</rss>

