<?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: Update the value of a variable based on the value of a variable of another dataset in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Update-the-value-of-a-variable-based-on-the-value-of-a-variable/m-p/943816#M42403</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/134532"&gt;@NewUsrStat&lt;/a&gt;, it isn't immediately clear what your desired outcome should be. From your initial description, of wanting to compare values in different datasets, it seems like you're trying to do a merge by ID, admission and discharge date. So far so good, you can achieve that with a DATA step merge (of course, making sure data is pre-sorted):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	merge db db1;
	by id admission discharge;
	format admission discharge date9.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, you also mention that the output dataset should contain a new variable (which you don't provide an example for). In your last dataset DB2, you have examples that don't quite align with your previous observations in DB and DB1. For example, how did we get this observation in DB2?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;166  16FEB2019 26FEB2019  1 0&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This observation doesn't meet the requirement specified:&amp;nbsp;&lt;SPAN&gt;only the case in which&amp;nbsp;&lt;/SPAN&gt;&lt;CODE class="  language-sas"&gt;Variable_&lt;/CODE&gt;&lt;SPAN&gt;=1 and&amp;nbsp;&lt;/SPAN&gt;&lt;CODE class="  language-sas"&gt;Variable&lt;/CODE&gt;&lt;SPAN&gt;=0.&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 13 Sep 2024 15:46:54 GMT</pubDate>
    <dc:creator>antonbcristina</dc:creator>
    <dc:date>2024-09-13T15:46:54Z</dc:date>
    <item>
      <title>Update the value of a variable based on the value of a variable of another dataset</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Update-the-value-of-a-variable-based-on-the-value-of-a-variable/m-p/943814#M42402</link>
      <description>&lt;P&gt;Hi guys,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;suppose to have the following datasets:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DB;
  input ID :$20. Admission :date09. Discharge :date09. Variable;
cards;
166  16FEB2019 26FEB2019  1
170  13JAN2017 25JAN2017  0
170  22FEB2017 07MAR2017  1
170  27APR2017 16MAY2017  1
170  30JAN2019 04MAR2019  0
313  03MAR2016 10MAR2016  1
; 
run;

data DB1;
  input ID :$20. Admission :date09. Discharge :date09. Variable_;
cards;
166 16FEB2019 26FEB2019 1
166 16FEB2019 26FEB2019 1
166 16FEB2019 26FEB2019 1
166 16FEB2019 26FEB2019 1
166 16FEB2019 26FEB2019 1
166 16FEB2019 26FEB2019 1
170 22FEB2017 07MAR2017 0
170 13JAN2017 25JAN2017 0
170 30JAN2019 04MAR2019 1
170 13JAN2017 25JAN2017 0 
170 30JAN2019 04MAR2019 1
170 27APR2017 16MAY2017 1
170 27APR2017 16MAY2017 1
170 27APR2017 16MAY2017 1
170 27APR2017 16MAY2017 1
170 27APR2017 16MAY2017 1
313 03MAR2016 10MAR2016 0
313 03MAR2016 10MAR2016 0
313 03MAR2016 10MAR2016 0
; 
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Is there a way to compare the Variable_ value in DB1 with Variable in DB at corresponding date and BY ID? As you can see in DB1 some dates are replicated and when it happens the value of Variable_ is also replicated so no chance to have 1 or 0 when dates are replicated but always 1 or always 0. the desired output should be:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DB2;
  input ID :$20. Admission :date09. Discharge :date09. Variable New_Var;
cards;
166  16FEB2019 26FEB2019  1 0
170  13JAN2017 25JAN2017  0 0
170  22FEB2017 07MAR2017  1 0
170  27APR2017 16MAY2017  1 0
170  30JAN2019 04MAR2019  0 1
313  03MAR2016 10MAR2016  1 0
; 
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In other words a new variable should be created in DB, called "&lt;CODE class=" language-sas"&gt;New_Var&lt;/CODE&gt;" equal to 1 only if Variable_ = 1 in DB1 and Variable = 0 in DB.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you in advance&lt;/P&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;</description>
      <pubDate>Fri, 13 Sep 2024 16:29:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Update-the-value-of-a-variable-based-on-the-value-of-a-variable/m-p/943814#M42402</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2024-09-13T16:29:29Z</dc:date>
    </item>
    <item>
      <title>Re: Update the value of a variable based on the value of a variable of another dataset</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Update-the-value-of-a-variable-based-on-the-value-of-a-variable/m-p/943816#M42403</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/134532"&gt;@NewUsrStat&lt;/a&gt;, it isn't immediately clear what your desired outcome should be. From your initial description, of wanting to compare values in different datasets, it seems like you're trying to do a merge by ID, admission and discharge date. So far so good, you can achieve that with a DATA step merge (of course, making sure data is pre-sorted):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	merge db db1;
	by id admission discharge;
	format admission discharge date9.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, you also mention that the output dataset should contain a new variable (which you don't provide an example for). In your last dataset DB2, you have examples that don't quite align with your previous observations in DB and DB1. For example, how did we get this observation in DB2?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;166  16FEB2019 26FEB2019  1 0&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This observation doesn't meet the requirement specified:&amp;nbsp;&lt;SPAN&gt;only the case in which&amp;nbsp;&lt;/SPAN&gt;&lt;CODE class="  language-sas"&gt;Variable_&lt;/CODE&gt;&lt;SPAN&gt;=1 and&amp;nbsp;&lt;/SPAN&gt;&lt;CODE class="  language-sas"&gt;Variable&lt;/CODE&gt;&lt;SPAN&gt;=0.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Sep 2024 15:46:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Update-the-value-of-a-variable-based-on-the-value-of-a-variable/m-p/943816#M42403</guid>
      <dc:creator>antonbcristina</dc:creator>
      <dc:date>2024-09-13T15:46:54Z</dc:date>
    </item>
    <item>
      <title>Re: Update the value of a variable based on the value of a variable of another dataset</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Update-the-value-of-a-variable-based-on-the-value-of-a-variable/m-p/943817#M42404</link>
      <description>&lt;P&gt;Which "corresponding date"? Both of your data sets have two date values. So there are potentially 4 pairs of values to be compared/matched on any given pair of observations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do yourself a favor and assign Formats to date values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now you get to explain why these values appear in the wanted set:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;166  16FEB2019 26FEB2019  1 0
170  30JAN2019 04MAR2019  0 1&lt;/PRE&gt;
&lt;P&gt;In the example set DB1 every value of variable+ for id 166 with dates of Admission =16FEB2019 and Discharge=26FEB2019 is 1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For Id=170 Admission=30JAN2019 and Discharge=04MAR2019 are all 0.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;IF your want data is incorrect and should have different values for variable_ then perhaps:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sort data=db;
   by id admission discharge;
run;
proc sort data=db1;
   by id admission discharge;
run;

data db2;
   update db (in=indb)
          db1
   ;
   by id admission discharge;
   if indb;
run;&lt;/PRE&gt;
&lt;P&gt;UPDATE only works if the combination of the BY variables only occurs exactly one time in the first data set on the UPDATE statement. Data should be sorted by those variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What this does is take the value of Variable_ and add it to the master record in DB each time it appears in DB1.&lt;/P&gt;
&lt;P&gt;IF you have multiple &lt;STRONG&gt;different&lt;/STRONG&gt; values of Variable_ the last one in DB1 would be the result.&lt;/P&gt;
&lt;P&gt;The IN=data set option creates a temporary variable that indicates the current observation comes from DB. So if there are ID and/or dates in DB1 that do not match one in DB they are not added to the desired output (The IF Indb; statement subsets data to only the ones that come from DB).&lt;/P&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;</description>
      <pubDate>Fri, 13 Sep 2024 15:48:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Update-the-value-of-a-variable-based-on-the-value-of-a-variable/m-p/943817#M42404</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-09-13T15:48:42Z</dc:date>
    </item>
    <item>
      <title>Re: Update the value of a variable based on the value of a variable of another dataset</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Update-the-value-of-a-variable-based-on-the-value-of-a-variable/m-p/943820#M42405</link>
      <description>&lt;P&gt;Thank you very much for your feedback. The new variable Variable_ in DB should be added after the comparison variable (from DB) vs Variable_ (from DB2). Briefly I need to update the variable "Variable" in DB only when value is 1 in DB1. I though to add a new variable called "Variable_" in DB and after run an if statement. With the merge replicated rows appear.&lt;/P&gt;</description>
      <pubDate>Fri, 13 Sep 2024 16:09:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Update-the-value-of-a-variable-based-on-the-value-of-a-variable/m-p/943820#M42405</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2024-09-13T16:09:10Z</dc:date>
    </item>
    <item>
      <title>Re: Update the value of a variable based on the value of a variable of another dataset</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Update-the-value-of-a-variable-based-on-the-value-of-a-variable/m-p/943821#M42406</link>
      <description>&lt;P&gt;I think it would be clearer if we used a new variable name, say New_Var. Is your requirement as follows:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Update DB by adding new variable New_Var&lt;/LI&gt;
&lt;LI&gt;New_Var should be set to 1 if Variable_=1 in DB1 for the same ID, admission and discharge date?&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Sep 2024 16:11:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Update-the-value-of-a-variable-based-on-the-value-of-a-variable/m-p/943821#M42406</guid>
      <dc:creator>antonbcristina</dc:creator>
      <dc:date>2024-09-13T16:11:30Z</dc:date>
    </item>
    <item>
      <title>Re: Update the value of a variable based on the value of a variable of another dataset</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Update-the-value-of-a-variable-based-on-the-value-of-a-variable/m-p/943822#M42407</link>
      <description>"UPDATE only works if the combination of the BY variables only occurs exactly one time in the first data set on the UPDATE statement." Unfortunately there are some replicates in DB but fortunately with identical values of "Variable"</description>
      <pubDate>Fri, 13 Sep 2024 16:13:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Update-the-value-of-a-variable-based-on-the-value-of-a-variable/m-p/943822#M42407</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2024-09-13T16:13:54Z</dc:date>
    </item>
    <item>
      <title>Re: Update the value of a variable based on the value of a variable of another dataset</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Update-the-value-of-a-variable-based-on-the-value-of-a-variable/m-p/943823#M42408</link>
      <description>EXACTLY!!!!!</description>
      <pubDate>Fri, 13 Sep 2024 16:14:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Update-the-value-of-a-variable-based-on-the-value-of-a-variable/m-p/943823#M42408</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2024-09-13T16:14:40Z</dc:date>
    </item>
    <item>
      <title>Re: Update the value of a variable based on the value of a variable of another dataset</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Update-the-value-of-a-variable-based-on-the-value-of-a-variable/m-p/943824#M42409</link>
      <description>&lt;P&gt;Unfortunately your example output dataset (DB2) does not follow this requirement. Would you mind running through this requirement for IDs 166 and 170 to confirm. I'm getting different answers.&lt;/P&gt;</description>
      <pubDate>Fri, 13 Sep 2024 16:21:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Update-the-value-of-a-variable-based-on-the-value-of-a-variable/m-p/943824#M42409</guid>
      <dc:creator>antonbcristina</dc:creator>
      <dc:date>2024-09-13T16:21:40Z</dc:date>
    </item>
    <item>
      <title>Re: Update the value of a variable based on the value of a variable of another dataset</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Update-the-value-of-a-variable-based-on-the-value-of-a-variable/m-p/943827#M42410</link>
      <description>&lt;P&gt;Example: pts 166: nothing changes (Variable_ and Variable both = 1) so in DB New_Var = 0, and so on until pts 170, 30JAN2019: Variable = 0 and Variable_ = 1 so New_Var = 1. There was a mistake in DB1. I'm really sorry.&lt;/P&gt;</description>
      <pubDate>Fri, 13 Sep 2024 16:30:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Update-the-value-of-a-variable-based-on-the-value-of-a-variable/m-p/943827#M42410</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2024-09-13T16:30:22Z</dc:date>
    </item>
    <item>
      <title>Re: Update the value of a variable based on the value of a variable of another dataset</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Update-the-value-of-a-variable-based-on-the-value-of-a-variable/m-p/943828#M42411</link>
      <description>&lt;P&gt;Hahaha, ok that makes more sense. In that case, you just want to add an IF statement after your merge, specifying the condition:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;	if variable=0 and variable_=1 then new_var=1;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 13 Sep 2024 16:35:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Update-the-value-of-a-variable-based-on-the-value-of-a-variable/m-p/943828#M42411</guid>
      <dc:creator>antonbcristina</dc:creator>
      <dc:date>2024-09-13T16:35:13Z</dc:date>
    </item>
    <item>
      <title>Re: Update the value of a variable based on the value of a variable of another dataset</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Update-the-value-of-a-variable-based-on-the-value-of-a-variable/m-p/943829#M42412</link>
      <description>Yes I know. My problem is to manage two datasets and the comparison of relative variables.</description>
      <pubDate>Fri, 13 Sep 2024 16:44:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Update-the-value-of-a-variable-based-on-the-value-of-a-variable/m-p/943829#M42412</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2024-09-13T16:44:10Z</dc:date>
    </item>
  </channel>
</rss>

