<?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: one to many update in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/one-to-many-update/m-p/935999#M367945</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/40498"&gt;@jcis7&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks for pointing out I forgot to make variableX character.&lt;/P&gt;
&lt;P&gt;I need to update variableX only for years 2020-21 through 2023-24 so wondering how I do that.&amp;nbsp; Thanks.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Having sneakily included an IF statement we just add to the conditions:&lt;/P&gt;
&lt;PRE&gt;data want;
   merge fileb 
         filea (keep= facility_code  variablex rename=(variablex=testx) )
   ;
   by facility_code ;
   if missing(variablex) and schoolyr in('2020-21' '2021-22' '2022-23' '2023-24') then variablex=testx;
   drop testx;
run;&lt;/PRE&gt;
&lt;P&gt;Suggestion: You may want to save yourself some headaches by using a numeric year value and use a custom format that will display the 2020-21 values as needed. Long lists of school years such as in the IN comparison above could be reduced to schoolyr in (2020:2023) or similar range of integer values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is an example of creating a data set that will format school years starting in 1950 through 2150 in the 1950-51 appearance.&lt;/P&gt;
&lt;PRE&gt;data syrcntlin;
   fmtname='SchoolYr';
   type='N';
   do start=1950 to 2150;
      label=catx('-',put(start,f4.),put(mod(start+1,100),z2.));
      output;
   end;
run;

proc format library=work cntlin=syrcntlin;
run;&lt;/PRE&gt;
&lt;P&gt;Just add: Format &amp;lt;year variable name&amp;gt; schoolyr. ; to any place you want the year to appear with that 2020-21 layout.&lt;/P&gt;
&lt;P&gt;I've worked with school year data for nearly 20 years and this approach has saved a lot of headaches in that time.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 17 Jul 2024 07:21:53 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2024-07-17T07:21:53Z</dc:date>
    <item>
      <title>one to many update</title>
      <link>https://communities.sas.com/t5/SAS-Programming/one-to-many-update/m-p/935964#M367930</link>
      <description>&lt;P&gt;G'day.&lt;/P&gt;
&lt;P&gt;Appreciate any help you can give.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've a file named fileA that has a unique identifying variable facility_code, no duplicates and it has variableX that I need to populate in fileB for certain years.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've a second file named fileB (combined years of data) with the&amp;nbsp; facility_code listed once for each year so since the file which has 7 years worth of data, if the facility reported information each year, it will&amp;nbsp; be listed 7 times.&amp;nbsp; It may not have reported each of the years, only some.&lt;/P&gt;
&lt;P&gt;fileB is missing data for variable X in years 2020-21 through 2023-24.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a variable in fileA named variableX I want to populate in fileB for only 4 of the seven school years (2020-21, 2021-22, 2022-23, 2023-24).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How do i update fileB, specifically variable X only for the four school years with the information&amp;nbsp; in FileA matched on facility_code for variable X only for specific school years?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I only know how to update the file if it is a one to one relationship in each file, for example if the facilty_code is listed only once in fileZ, code below, I know how to update it with the information in fileA (2nd SAS code window below).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;thanks!&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA fileA;
 INPUT facility_code $ facility_type$ physical_zip$ variableX schoolyr$;
CARDS;
1234567 public 14524 Yes 2019-20
1234568 public 23561 No 2019-20
1234569 public 84972 No 2019-20
0234567 public 94801 Yes 2019-20
0234568 public 89024 No 2019-20 
0234569 public 28201 No 2019-20

;
RUN; 


DATA fileB;
 INPUT facility_code $ status$ facility_type$ physical_zip$ variableX schoolyr$;
CARDS;
1234567 active public 14524 Yes 2017-18
1234567 closed public 14524 Yes 2018-19
1234567 active public 14524 No  2017-18
1234567 closed public 14524 . 2021-22
1234568 active public 23561 Yes 2017-18
1234568 active public 23561 Yes 2018-19
1234568 active public 23561 Yes 2019-20
1234568 active public 23561 . 2020-21
1234568 active public 23561 . 2021-22
1234568 active public 23561 . 2022-23
1234568 active public 23561 . 2023-24
1234568 active public 23561 . 2018-19
1234569 closed public 84972 . 2020-21
1234569 closed public 84972 . 2021-22
1234569 closed public 84972 . 2022-23
1234569 closed public 84972 . 2023-24
0234567 active public 94801 . 2021-22
0234568 active public 89024 . 2022-23
0234569 active public 28201 . 2023-24

;
RUN; 



DATA want;
 INPUT facility_code $ status$ facility_type$ physical_zip$ variableX $ schoolyr$;
CARDS;
1234567 active public 14524 Yes 2017-18
1234567 closed public 14524 Yes 2018-19
1234567 active public 14524 No 2017-18
1234567 closed public 14524 Yes 2021-22
1234568 active public 23561 Yes 2017-18
1234568 active public 23561 Yes 2018-19
1234568 active public 23561 No 2019-20
1234568 active public 23561 No 2020-21
1234568 active public 23561 No 2021-22
1234568 active public 23561 No 2022-23
1234568 active public 23561 No 2023-24
1234568 active public 23561 No 2018-19
1234569 closed public 84972 No 2020-21
1234569 closed public 84972 No 2021-22
1234569 closed public 84972 No 2022-23
1234569 closed public 84972 No 2023-24
0234567 active public 94801 Yes 2021-22
0234568 active public 89024 No 2022-23
0234569 active public 28201 No 2023-24

;
RUN; 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data transaction;
  set fileA (keep=facility_code variableX);
run;

*--sort transaction;
proc sort data=transaction;
  by facility_code;
run;

*--sort list to be updated;
proc sort data=fileZ;
  by facility_code;
run;

*-update;
data updatedZ;
   update fileZ transaction updatemode=missingcheck;   
   by facility_code;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Jul 2024 22:07:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/one-to-many-update/m-p/935964#M367930</guid>
      <dc:creator>jcis7</dc:creator>
      <dc:date>2024-07-16T22:07:35Z</dc:date>
    </item>
    <item>
      <title>Re: one to many update</title>
      <link>https://communities.sas.com/t5/SAS-Programming/one-to-many-update/m-p/935966#M367932</link>
      <description>&lt;P&gt;Your first data step throws invalid data for every observation for variableX as you have not told SAS to read it as character.&lt;/P&gt;
&lt;P&gt;Your second data step reads variablex as numeric.&lt;/P&gt;
&lt;P&gt;You cannot update a numeric variable with a character value of "Yes" or "No".&lt;/P&gt;
&lt;P&gt;So what are we missing? Is Variablex supposed to be numeric or character?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The UPDATE data step statement requires the target set to have only one value.&lt;/P&gt;
&lt;P&gt;You are close. MERGE will handle this if only one set has duplicate by variables:&lt;/P&gt;
&lt;PRE&gt;data want;
   merge fileb 
         filea (keep= facility_code  variablex rename=(variablex=testx) )
   ;
   by facility_code ;
   if missing(variablex) then variablex=testx;
   drop testx;
run;
&lt;/PRE&gt;
&lt;P&gt;You didn't mention what the behavior for the variables facility_type physical_zip or school_yr should be from FileA so I dropped them. The code i have is set up to handle a FileB where some of the values of VariableX are not missing. Because the MERGE be default would replace all of them I renamed the variable in data set options to something else and then only use the renamed value to replace missing variablex when encountered.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that the MISSING function will work regardless of variable type so no need to modify after you decide whether VariableX is character or numeric.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Jul 2024 22:25:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/one-to-many-update/m-p/935966#M367932</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-07-16T22:25:18Z</dc:date>
    </item>
    <item>
      <title>Re: one to many update</title>
      <link>https://communities.sas.com/t5/SAS-Programming/one-to-many-update/m-p/935970#M367933</link>
      <description>&lt;P&gt;Thanks for pointing out I forgot to make variableX character.&lt;/P&gt;
&lt;P&gt;I need to update variableX only for years 2020-21 through 2023-24 so wondering how I do that.&amp;nbsp; Thanks.&lt;/P&gt;</description>
      <pubDate>Tue, 16 Jul 2024 22:47:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/one-to-many-update/m-p/935970#M367933</guid>
      <dc:creator>jcis7</dc:creator>
      <dc:date>2024-07-16T22:47:52Z</dc:date>
    </item>
    <item>
      <title>Re: one to many update</title>
      <link>https://communities.sas.com/t5/SAS-Programming/one-to-many-update/m-p/935999#M367945</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/40498"&gt;@jcis7&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks for pointing out I forgot to make variableX character.&lt;/P&gt;
&lt;P&gt;I need to update variableX only for years 2020-21 through 2023-24 so wondering how I do that.&amp;nbsp; Thanks.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Having sneakily included an IF statement we just add to the conditions:&lt;/P&gt;
&lt;PRE&gt;data want;
   merge fileb 
         filea (keep= facility_code  variablex rename=(variablex=testx) )
   ;
   by facility_code ;
   if missing(variablex) and schoolyr in('2020-21' '2021-22' '2022-23' '2023-24') then variablex=testx;
   drop testx;
run;&lt;/PRE&gt;
&lt;P&gt;Suggestion: You may want to save yourself some headaches by using a numeric year value and use a custom format that will display the 2020-21 values as needed. Long lists of school years such as in the IN comparison above could be reduced to schoolyr in (2020:2023) or similar range of integer values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is an example of creating a data set that will format school years starting in 1950 through 2150 in the 1950-51 appearance.&lt;/P&gt;
&lt;PRE&gt;data syrcntlin;
   fmtname='SchoolYr';
   type='N';
   do start=1950 to 2150;
      label=catx('-',put(start,f4.),put(mod(start+1,100),z2.));
      output;
   end;
run;

proc format library=work cntlin=syrcntlin;
run;&lt;/PRE&gt;
&lt;P&gt;Just add: Format &amp;lt;year variable name&amp;gt; schoolyr. ; to any place you want the year to appear with that 2020-21 layout.&lt;/P&gt;
&lt;P&gt;I've worked with school year data for nearly 20 years and this approach has saved a lot of headaches in that time.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jul 2024 07:21:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/one-to-many-update/m-p/935999#M367945</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-07-17T07:21:53Z</dc:date>
    </item>
  </channel>
</rss>

