<?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: Need help to update values in the table based on another values in the same table in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Need-help-to-update-values-in-the-table-based-on-another-values/m-p/907886#M358351</link>
    <description>&lt;P&gt;My approach.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Identify unique MS_POL with at least one observation with SUG_TAARIF=9&lt;/P&gt;
&lt;P&gt;2. Get KOD_TAARIF values for those observations into macro variable&lt;/P&gt;
&lt;P&gt;3. Assign macro variable values to KOD_TAARIF where MS_SEIF=17, appropriately per MS_POL .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data Have;
Input MS_POL MS_SEIF KOD_TAARIF SUG_TAARIF;
datalines;
15476381 1 758 7
15476381 1 772 9
15476381 17 0 0
15476382 1 3001 7
15476382 1 3002 9
15476382 17 0 0
;
proc print; Run;


*Identifying unique MS_POls having at least one record where SUG_TAARIF = 9;
proc sql noprint;;
	select distinct MS_POL into:id separated by ' ' from have where SUG_TAARIF = 9;
quit;
%put &amp;amp;=id;  

*Splitting data into 2 parts, one with SUG_TAARIF = 9 obs, another without SUG_TAARIF = 9 observation; 
data have1 have2;
	set have;
	if MS_POL in (&amp;amp;id) then output have1;
	else output have2;
run; 

*Creating series of macro variables with KOD_TAARIF values where SUG_TAARIF = 9; 
data _null_;
	set have1;
	if SUG_TAARIF= 9 then call symputx(cats('kt', MS_POL), KOD_TAARIF);
run; 
%put &amp;amp;=kt15476381, &amp;amp;=kt15476382;


*If MS_SEIF = 17 then bring KOD_TAARIF from the Same MS_POL where SUG_TAARIF = 9.;
data have1a;
	set have1;
	if ms_seif eq 17 then do;
		sug_taarif=9;
		kod_taarif=input(symget(cats('kt', ms_pol)), best.); 
	end; 
run;

*Setting back two parts of HAVE dataset;
data want;
	set have1a have2;
proc print;run;  &lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 13 Dec 2023 18:48:20 GMT</pubDate>
    <dc:creator>A_Kh</dc:creator>
    <dc:date>2023-12-13T18:48:20Z</dc:date>
    <item>
      <title>Need help to update values in the table based on another values in the same table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-to-update-values-in-the-table-based-on-another-values/m-p/907738#M358314</link>
      <description>&lt;P&gt;Hi!&lt;/P&gt;
&lt;P&gt;I have data like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data Have;
Input MS_POL MS_SEIF KOD_TAARIF SUG_TAARIF;
datalines;
15476381 1 758 7
15476381 1 772 9
15476381 17 0 0
15476382 1 3001 7
15476382 1 3002 9
15476382 17 0 0
;
Run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I need data like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data Want;
Input MS_POL MS_SEIF KOD_TAARIF SUG_TAARIF;
datalines;
15476381 1 758 7
15476381 1 772 9
15476381 17 772 9
15476382 1 3001 7
15476382 1 3002 9
15476382 17 3002 9
;
Run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The logic:&lt;/P&gt;
&lt;P&gt;If MS_SEIF = 17 then bring KOD_TAARIF from the Same MS_POL where SUG_TAARIF = 9.&lt;/P&gt;
&lt;P&gt;If There is no&amp;nbsp;SUG_TAARIF = 9 Then&amp;nbsp;KOD_TAARIF&amp;nbsp; = 0&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any help how to do that will be highly appreciated.&lt;/P&gt;
&lt;P&gt;Thanks in advance.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Dec 2023 13:23:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-to-update-values-in-the-table-based-on-another-values/m-p/907738#M358314</guid>
      <dc:creator>IgorR</dc:creator>
      <dc:date>2023-12-13T13:23:52Z</dc:date>
    </item>
    <item>
      <title>Re: Need help to update values in the table based on another values in the same table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-to-update-values-in-the-table-based-on-another-values/m-p/907739#M358315</link>
      <description>&lt;P&gt;Will the data always be sorted as you have shown? Or can sometimes a different sort be found where MS_SEIF=17 is not the last row for a given MS_POL? Is SUG_TAARIF always in the order shown, or can that change too?&lt;/P&gt;</description>
      <pubDate>Wed, 13 Dec 2023 13:37:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-to-update-values-in-the-table-based-on-another-values/m-p/907739#M358315</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-12-13T13:37:01Z</dc:date>
    </item>
    <item>
      <title>Re: Need help to update values in the table based on another values in the same table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-to-update-values-in-the-table-based-on-another-values/m-p/907740#M358316</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;,&amp;nbsp;I can't guarantee that the data will always be sorted.&lt;/P&gt;
&lt;P&gt;There might be additional data between these rows with different values.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Dec 2023 13:48:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-to-update-values-in-the-table-based-on-another-values/m-p/907740#M358316</guid>
      <dc:creator>IgorR</dc:creator>
      <dc:date>2023-12-13T13:48:17Z</dc:date>
    </item>
    <item>
      <title>Re: Need help to update values in the table based on another values in the same table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-to-update-values-in-the-table-based-on-another-values/m-p/907741#M358317</link>
      <description>&lt;P&gt;If our solution were to sort the data, would that work for you? How large is the actual data set (number of rows and number of columns)?&lt;/P&gt;</description>
      <pubDate>Wed, 13 Dec 2023 13:50:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-to-update-values-in-the-table-based-on-another-values/m-p/907741#M358317</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-12-13T13:50:46Z</dc:date>
    </item>
    <item>
      <title>Re: Need help to update values in the table based on another values in the same table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-to-update-values-in-the-table-based-on-another-values/m-p/907742#M358318</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;, Depends of the month - between 10 to 20 million rows.&lt;/P&gt;
&lt;P&gt;Constant 18 columns.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Dec 2023 13:55:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-to-update-values-in-the-table-based-on-another-values/m-p/907742#M358318</guid>
      <dc:creator>IgorR</dc:creator>
      <dc:date>2023-12-13T13:55:15Z</dc:date>
    </item>
    <item>
      <title>Re: Need help to update values in the table based on another values in the same table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-to-update-values-in-the-table-based-on-another-values/m-p/907743#M358319</link>
      <description>&lt;P&gt;&lt;SPAN&gt;If our solution were to sort the data, would that work for you?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Dec 2023 13:56:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-to-update-values-in-the-table-based-on-another-values/m-p/907743#M358319</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-12-13T13:56:52Z</dc:date>
    </item>
    <item>
      <title>Re: Need help to update values in the table based on another values in the same table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-to-update-values-in-the-table-based-on-another-values/m-p/907745#M358321</link>
      <description>&lt;P&gt;If it works, yes.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Dec 2023 14:05:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-to-update-values-in-the-table-based-on-another-values/m-p/907745#M358321</guid>
      <dc:creator>IgorR</dc:creator>
      <dc:date>2023-12-13T14:05:24Z</dc:date>
    </item>
    <item>
      <title>Re: Need help to update values in the table based on another values in the same table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-to-update-values-in-the-table-based-on-another-values/m-p/907750#M358322</link>
      <description>&lt;P&gt;Well, I will have to think about this, whatever solution I come up with for your sample data also needs to be scaled and run efficiently for 10-20 million records, and at the moment I'm not sure how best to do this.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Dec 2023 14:26:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-to-update-values-in-the-table-based-on-another-values/m-p/907750#M358322</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-12-13T14:26:11Z</dc:date>
    </item>
    <item>
      <title>Re: Need help to update values in the table based on another values in the same table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-to-update-values-in-the-table-based-on-another-values/m-p/907886#M358351</link>
      <description>&lt;P&gt;My approach.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Identify unique MS_POL with at least one observation with SUG_TAARIF=9&lt;/P&gt;
&lt;P&gt;2. Get KOD_TAARIF values for those observations into macro variable&lt;/P&gt;
&lt;P&gt;3. Assign macro variable values to KOD_TAARIF where MS_SEIF=17, appropriately per MS_POL .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data Have;
Input MS_POL MS_SEIF KOD_TAARIF SUG_TAARIF;
datalines;
15476381 1 758 7
15476381 1 772 9
15476381 17 0 0
15476382 1 3001 7
15476382 1 3002 9
15476382 17 0 0
;
proc print; Run;


*Identifying unique MS_POls having at least one record where SUG_TAARIF = 9;
proc sql noprint;;
	select distinct MS_POL into:id separated by ' ' from have where SUG_TAARIF = 9;
quit;
%put &amp;amp;=id;  

*Splitting data into 2 parts, one with SUG_TAARIF = 9 obs, another without SUG_TAARIF = 9 observation; 
data have1 have2;
	set have;
	if MS_POL in (&amp;amp;id) then output have1;
	else output have2;
run; 

*Creating series of macro variables with KOD_TAARIF values where SUG_TAARIF = 9; 
data _null_;
	set have1;
	if SUG_TAARIF= 9 then call symputx(cats('kt', MS_POL), KOD_TAARIF);
run; 
%put &amp;amp;=kt15476381, &amp;amp;=kt15476382;


*If MS_SEIF = 17 then bring KOD_TAARIF from the Same MS_POL where SUG_TAARIF = 9.;
data have1a;
	set have1;
	if ms_seif eq 17 then do;
		sug_taarif=9;
		kod_taarif=input(symget(cats('kt', ms_pol)), best.); 
	end; 
run;

*Setting back two parts of HAVE dataset;
data want;
	set have1a have2;
proc print;run;  &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 13 Dec 2023 18:48:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-to-update-values-in-the-table-based-on-another-values/m-p/907886#M358351</guid>
      <dc:creator>A_Kh</dc:creator>
      <dc:date>2023-12-13T18:48:20Z</dc:date>
    </item>
    <item>
      <title>Re: Need help to update values in the table based on another values in the same table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-to-update-values-in-the-table-based-on-another-values/m-p/907962#M358371</link>
      <description>&lt;P&gt;Below approach doesn't require sorting and given your volumes you should also have enough memory for the hash table.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data Have;
  Input MS_POL MS_SEIF KOD_TAARIF SUG_TAARIF;
  datalines;
15476381 1 758 7
15476381 1 772 9
15476381 17 0 0
15476382 1 3001 7
15476382 1 3002 9
15476382 17 0 0
;

data want;
  if _n_=1 then
    do;
      dcl hash h1(dataset:'have(where=(SUG_TAARIF = 9))');
      h1.defineKey('MS_POL');
      h1.defineData('KOD_TAARIF ');
      h1.defineDone();
    end;
  set have;
  If MS_SEIF = 17 then
    do;
      if h1.find() ne 0 then KOD_TAARIF = 0;
    end;
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1702517321830.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/91360i43FDBF073CB6F776/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1702517321830.png" alt="Patrick_0-1702517321830.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Dec 2023 01:28:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-to-update-values-in-the-table-based-on-another-values/m-p/907962#M358371</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-12-14T01:28:48Z</dc:date>
    </item>
    <item>
      <title>Re: Need help to update values in the table based on another values in the same table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-to-update-values-in-the-table-based-on-another-values/m-p/908514#M358517</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;Thank you for your help!&lt;/P&gt;
&lt;P&gt;I have a question - must "h1.defineData('KOD_TAARIF'); " have the same name in the both tables?&lt;/P&gt;
&lt;P&gt;What should I do If it has different names in two tables?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 17 Dec 2023 10:57:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-to-update-values-in-the-table-based-on-another-values/m-p/908514#M358517</guid>
      <dc:creator>IgorR</dc:creator>
      <dc:date>2023-12-17T10:57:51Z</dc:date>
    </item>
    <item>
      <title>Re: Need help to update values in the table based on another values in the same table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-to-update-values-in-the-table-based-on-another-values/m-p/908521#M358520</link>
      <description>&lt;P&gt;They can have different names but if there is no same named variable the Base dataset then you need to also define it there (for the PDV).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The easiest is to rename the variable before loading into the hash.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  if _n_=1 then
    do;
      dcl hash h1(dataset:'looup_table(where=(SUG_TAARIF = 9) renanme=(other_name=KOD_TAARIF))');
      h1.defineKey('MS_POL');
      h1.defineData('KOD_TAARIF ');
      h1.defineDone();
    end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If different name then define the variable also for the PDV&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  if _n_=1 then
    do;
      length other_name 8
      dcl hash h1(dataset:'looup_table(where=(SUG_TAARIF = 9))');
      h1.defineKey('MS_POL');
      h1.defineData('other_name ');
      h1.defineDone();
    end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or this way to create the variable with the exact attributes like in source&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  if _n_=1 then
    do;
      if 0 then set looup_table(keep=other_name);
      dcl hash h1(dataset:'looup_table(where=(SUG_TAARIF = 9))');
      h1.defineKey('MS_POL');
      h1.defineData('other_name ');
      h1.defineDone();
    end;
    call missing(other_name);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The call missing() is required because with the variable defined via a SET statement it gets retained if there is no new row read from the source table (which with if 0 then.. never happens).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Without the call missing() and if the hash lookup doesn't find an entry you would get the retained value from the last successful lookup.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the variable names in the hash lookup are different then you will also need to use the KEY keyword for lookup if it's a key variable and also DATA if it's a data variable. It's all explained in the docu and multiple good white papers (and there is also a book). It's really worth learning how hash tables work in SAS and what the use cases are.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 17 Dec 2023 20:11:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-to-update-values-in-the-table-based-on-another-values/m-p/908521#M358520</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-12-17T20:11:27Z</dc:date>
    </item>
  </channel>
</rss>

