<?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: Replacing value from specific row in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Replacing-value-from-specific-row/m-p/743162#M232597</link>
    <description>&lt;P&gt;I don't fully understand this. Why those exact observations?&lt;/P&gt;</description>
    <pubDate>Sat, 22 May 2021 18:50:48 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2021-05-22T18:50:48Z</dc:date>
    <item>
      <title>Replacing value from specific row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-value-from-specific-row/m-p/743139#M232591</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I have dataset example illustrated below:&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;row#&lt;/TD&gt;&lt;TD&gt;YEAR&lt;/TD&gt;&lt;TD&gt;SEX&lt;/TD&gt;&lt;TD&gt;CITY&lt;/TD&gt;&lt;TD&gt;STATISTICS&lt;/TD&gt;&lt;TD&gt;VALUE&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2021&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;500&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;2&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;2021&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD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&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;150&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;2021&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;550&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;11&lt;/TD&gt;&lt;TD&gt;2021&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;549&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;12&lt;/TD&gt;&lt;TD&gt;2021&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;'.' refers to the total&lt;/P&gt;&lt;P&gt;I would like to replace the value from row 3 to match with the row 1 value (500) in order to keep consistency on the total. Also, I would like to replace the value row 11 to match it with the row 10 value (550). Is there any code rather than inputting manually to replace values in this situation?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you,&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 22 May 2021 17:01:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-value-from-specific-row/m-p/743139#M232591</guid>
      <dc:creator>tmdgus</dc:creator>
      <dc:date>2021-05-22T17:01:27Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing value from specific row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-value-from-specific-row/m-p/743152#M232595</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;See below for two possible solutions.&lt;/P&gt;
&lt;P&gt;These are, of course, 2 hard-coded solutions!!&lt;/P&gt;
&lt;P&gt;Ideally, you would programmatically look at what the discrepancy is with the total and then proportionally increase (or decrease) the women and men until the total for both is reached. ****That is also easy to do.****&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input rownr YEAR SEX CITY STATISTICS VALUE;
cards;
1	2021	.	.	1	500
2   2021	1	1	1	0
3	2021	2	1	1	499
4	2021	.	1	2	750
5	2021	1	1	2	0
6	2021	2	1	2	750
7	2021	.	2	1	300
8	2021	1	2	1	150
9	2021	2	2	1	150
10	2021	.	2	2	550
11	2021	1	2	2	549
12	2021	2	2	2	0
;
run;

data want1;
 set have;
 by rownr year sex;
 if      _N_=3  then value=500;
 else if _N_=11 then value=550;
 else;
run;

data want2(drop=valueretain);
 set have;
 by rownr year sex;
 retain valueretain .;
 if      _N_=1  then valueretain=value;
 else if _N_=3  then do; value=valueretain; valueretain=.; end;
 else if _N_=10 then valueretain=value;
 else if _N_=11 then do; value=valueretain; valueretain=.; end;
 else;
run;

proc compare data=want1 compare=want2;
run;
/* end of program */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheers,&lt;/P&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
      <pubDate>Sat, 22 May 2021 18:07:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-value-from-specific-row/m-p/743152#M232595</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2021-05-22T18:07:37Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing value from specific row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-value-from-specific-row/m-p/743162#M232597</link>
      <description>&lt;P&gt;I don't fully understand this. Why those exact observations?&lt;/P&gt;</description>
      <pubDate>Sat, 22 May 2021 18:50:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-value-from-specific-row/m-p/743162#M232597</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-05-22T18:50:48Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing value from specific row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-value-from-specific-row/m-p/743166#M232598</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;I see it like this.&lt;/P&gt;
&lt;P&gt;For every&amp;nbsp;CITY STATISTICS combination, there's a total (first row with sex=.) and a sex=1 value and a sex=2 value. The sex=1 value + sex=2 value should equal the total.&lt;/P&gt;
&lt;P&gt;I think the CITY value for row 1 should be 1 instead of . (missing).&lt;/P&gt;
&lt;P&gt;The original questioner&amp;nbsp;can contradict me of course.&amp;nbsp;Do not take my word for it in this matter.&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":smirking_face:"&gt;😏&lt;/span&gt;&lt;span class="lia-unicode-emoji" title=":smirking_face:"&gt;😏&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Cheers,&lt;/P&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
      <pubDate>Sat, 22 May 2021 19:10:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-value-from-specific-row/m-p/743166#M232598</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2021-05-22T19:10:41Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing value from specific row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-value-from-specific-row/m-p/743167#M232599</link>
      <description>&lt;P&gt;Thanks for the code. Would you share the sample of code for the last sentence you mentioned?&lt;/P&gt;&lt;P&gt;(&lt;SPAN&gt;Ideally, you would programmatically look at what the discrepancy is with the total and then proportionally increase (or decrease) the women and men until the total for both is reached. ****That is also easy to do.****)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thank you,&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 22 May 2021 19:33:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-value-from-specific-row/m-p/743167#M232599</guid>
      <dc:creator>tmdgus</dc:creator>
      <dc:date>2021-05-22T19:33:47Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing value from specific row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-value-from-specific-row/m-p/743168#M232600</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/60547"&gt;@sbxkoenk&lt;/a&gt;&amp;nbsp; Yes, you are correct. The city for the first row should be 1.&lt;/P&gt;</description>
      <pubDate>Sat, 22 May 2021 19:35:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-value-from-specific-row/m-p/743168#M232600</guid>
      <dc:creator>tmdgus</dc:creator>
      <dc:date>2021-05-22T19:35:25Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing value from specific row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-value-from-specific-row/m-p/743217#M232636</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/382980"&gt;@tmdgus&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;Since you have told me (in a private mail) that one of the 2 sexes always has a value of 0 if the sexes don't add up to the total, it's even easier.&lt;/P&gt;
&lt;P&gt;Here's the code:&lt;/P&gt;
&lt;P&gt;(the code can be more compact and elegant than below but I didn't want to make it too complicated)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC DATASETS library=WORK NoList;
 delete have: want: / memtype=DATA; run;
QUIT;

data have(drop=CITY STATISTICS);
LENGTH rownr YEAR 8
       SEX CITY STATISTICS $ 1 citystat $ 3
       VALUE 8;
input rownr YEAR SEX $ CITY $ STATISTICS $ VALUE;
citystat=CITY !! '|' !! STATISTICS;
if missing(sex) then sex='0';
cards;
1	2021	.	1	1	500
2   2021	1	1	1	0
3	2021	2	1	1	499
4	2021	.	1	2	750
5	2021	1	1	2	0
6	2021	2	1	2	750
7	2021	.	2	1	300
8	2021	1	2	1	150
9	2021	2	2	1	150
10	2021	.	2	2	550
11	2021	1	2	2	549
12	2021	2	2	2	0
;
run;

PROC TRANSPOSE data=have 
               out=have_trp1(drop=_NAME_)
               prefix=sex_;
 by YEAR citystat;
 id sex;
 var value;
run; 

data have_trp2(drop=deviance);
 set have_trp1;
 deviance=(sex_0 - SUM(sex_1,sex_2));
 if deviance ^= 0 then do;
  if      sex_1=0 then sex_2=(sex_2 + deviance);
  else if sex_2=0 then sex_1=(sex_1 + deviance);
  else;
                       end;
run;

data want(drop=i sex_0 sex_1 sex_2);
LENGTH YEAR 8
       SEX $ 1 citystat $ 3
       VALUE 8;
 set have_trp2;
 array sexarray{3} sex_0 sex_1 sex_2;
 do i=1 to dim(sexarray);
  sex=compress(vname(sexarray(i)),'sex_');
  if sex='0' then sex='.';
  value=sexarray(i);
  output;
 end;
run;
 /* end of program */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
      <pubDate>Sun, 23 May 2021 13:51:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-value-from-specific-row/m-p/743217#M232636</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2021-05-23T13:51:39Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing value from specific row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-value-from-specific-row/m-p/743264#M232668</link>
      <description>Thank you very much!</description>
      <pubDate>Sun, 23 May 2021 22:00:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-value-from-specific-row/m-p/743264#M232668</guid>
      <dc:creator>tmdgus</dc:creator>
      <dc:date>2021-05-23T22:00:20Z</dc:date>
    </item>
  </channel>
</rss>

