<?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: Accept first value when merging datasets in SAS Studio</title>
    <link>https://communities.sas.com/t5/SAS-Studio/Accept-first-value-when-merging-datasets/m-p/719226#M9741</link>
    <description>&lt;P&gt;Ah. My events actually aren't numbered. Sorry that was misleading.&lt;/P&gt;&lt;P&gt;I ended up subsetting, and then using %dropmiss to get rid of empty columns. I then used&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA data = WORK.want;

&amp;nbsp;merge WORK.have_event3 WORK.have_event2;

&amp;nbsp;by ID;

RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;To my understanding with how merge works, if Event 3 and Event 2 share the same name for a variable, then non-missing Event 2 observations should overwrite the data in Event 3. Please let me know if this understanding is incorrect.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 14 Feb 2021 17:25:16 GMT</pubDate>
    <dc:creator>amarikow57</dc:creator>
    <dc:date>2021-02-14T17:25:16Z</dc:date>
    <item>
      <title>Accept first value when merging datasets</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Accept-first-value-when-merging-datasets/m-p/719138#M9734</link>
      <description>&lt;P&gt;I am looking at subsets of data from a larger dataset to see which variables are contained in which event. The original data is structured similar to the following:&lt;/P&gt;&lt;P&gt;ID EVENT VAR1 VAR2 VAR3 VAR4 VAR5 ....&lt;/P&gt;&lt;P&gt;1&amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/P&gt;&lt;P&gt;1&amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;&lt;P&gt;1&amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 7&lt;/P&gt;&lt;P&gt;2&amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;&lt;P&gt;2&amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/P&gt;&lt;P&gt;3&amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;&lt;P&gt;3&amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/P&gt;&lt;P&gt;3&amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;6&lt;/P&gt;&lt;P&gt;etc.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;After I subset&amp;nbsp;by event using the WEHERE statement I plan to have something like:&lt;/P&gt;&lt;P&gt;Event 1&lt;/P&gt;&lt;P&gt;ID VAR1 VAR2 VAR3 VAR4 VAR5 ....&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Event 2&lt;/P&gt;&lt;P&gt;ID VAR1 VAR2 VAR3 VAR4 VAR5 ....&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Event 3&lt;/P&gt;&lt;P&gt;ID VAR1 VAR2 VAR3 VAR4 VAR5 ....&lt;/P&gt;&lt;P&gt;1&amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 7&lt;/P&gt;&lt;P&gt;3&amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;6&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would then like to&amp;nbsp;&lt;STRONG&gt;(1)&lt;/STRONG&gt; delete any column within each subset that does not contain any information,&amp;nbsp;then&amp;nbsp;&lt;STRONG&gt;(2)&amp;nbsp;&lt;/STRONG&gt;merge the datasets to where I only have one row per ID, while&amp;nbsp;&lt;STRONG&gt;(3)&lt;/STRONG&gt; maintaining the information of the left most dataset if there is information for a variable in both datasets. So merging Event 3 to Event 2, I would want to keep the VAR3 observations from Event 2 and not Event 3. Is there a way to do this? (&lt;U&gt;Note:&lt;/U&gt; actual dataset has about 100 variables) I suppose would it be better to right join Event 2 to Event 3 or left join Event 3 to Event 2?&lt;/P&gt;</description>
      <pubDate>Sat, 13 Feb 2021 19:54:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Accept-first-value-when-merging-datasets/m-p/719138#M9734</guid>
      <dc:creator>amarikow57</dc:creator>
      <dc:date>2021-02-13T19:54:26Z</dc:date>
    </item>
    <item>
      <title>Re: Accept first value when merging datasets</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Accept-first-value-when-merging-datasets/m-p/719177#M9740</link>
      <description>&lt;P&gt;If I understand your objective correctly, you can get there with a minimum of fuss.&amp;nbsp; Try:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have;
   by id descending event;
run;

data want (drop=event);
   update have (obs=0) have;
   by id;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This should work regardless of how many variables you have.&lt;/P&gt;</description>
      <pubDate>Sun, 14 Feb 2021 05:30:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Accept-first-value-when-merging-datasets/m-p/719177#M9740</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2021-02-14T05:30:29Z</dc:date>
    </item>
    <item>
      <title>Re: Accept first value when merging datasets</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Accept-first-value-when-merging-datasets/m-p/719226#M9741</link>
      <description>&lt;P&gt;Ah. My events actually aren't numbered. Sorry that was misleading.&lt;/P&gt;&lt;P&gt;I ended up subsetting, and then using %dropmiss to get rid of empty columns. I then used&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA data = WORK.want;

&amp;nbsp;merge WORK.have_event3 WORK.have_event2;

&amp;nbsp;by ID;

RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;To my understanding with how merge works, if Event 3 and Event 2 share the same name for a variable, then non-missing Event 2 observations should overwrite the data in Event 3. Please let me know if this understanding is incorrect.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 14 Feb 2021 17:25:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Accept-first-value-when-merging-datasets/m-p/719226#M9741</guid>
      <dc:creator>amarikow57</dc:creator>
      <dc:date>2021-02-14T17:25:16Z</dc:date>
    </item>
    <item>
      <title>Re: Accept first value when merging datasets</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Accept-first-value-when-merging-datasets/m-p/719236#M9746</link>
      <description>&lt;P&gt;No, your understanding is not correct.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Missing values in the second data set overwrite non-missing values in the first data set.&amp;nbsp; When you are unsure, you should actually test your program and see what happens.&amp;nbsp; You could certainly observe this happening.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You have some work to do.&amp;nbsp; MERGE is an important topic, one worth digging up documentation and papers, and studying.&amp;nbsp; There's no substitute.for doing that work.&amp;nbsp; I have to assume that since your understanding of MERGE is shaky, you have probably never heard of UPDATE.&amp;nbsp; That one is less common, but is the right tool for the job here.&amp;nbsp; Since you don't have EVENT in your data, I would just create it.&amp;nbsp; Any ordered values would do (they don't need to begin with 1 for each ID):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data events;
   set have;
   event + 1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then sort your data, and apply the program I gave you:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=events;
   by id descending event;
run;
data want (drop=event);
   update events (obs=0) events;
   by id;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 14 Feb 2021 21:50:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Accept-first-value-when-merging-datasets/m-p/719236#M9746</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2021-02-14T21:50:52Z</dc:date>
    </item>
    <item>
      <title>Re: Accept first value when merging datasets</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Accept-first-value-when-merging-datasets/m-p/719403#M9748</link>
      <description>&lt;P&gt;Okay. Thank you for clarifying that for me. I have an events variable but they are given character names. Would you recommend I use IF THEN statement to assign numerical value to them so they arrange in the order I want?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Additionally, so I understand the code correctly when asked about it, you are first sorting ID and then descending order of event (5, 4, 3, 2, 1). When you use UPDATE and (obs = 1), you are collapsing the IDs down to one where the bottom event replaces the prior event given that the bottom event has any values but skips if missing?&lt;/P&gt;</description>
      <pubDate>Mon, 15 Feb 2021 17:45:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Accept-first-value-when-merging-datasets/m-p/719403#M9748</guid>
      <dc:creator>amarikow57</dc:creator>
      <dc:date>2021-02-15T17:45:37Z</dc:date>
    </item>
    <item>
      <title>Re: Accept first value when merging datasets</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Accept-first-value-when-merging-datasets/m-p/719462#M9751</link>
      <description>&lt;P&gt;Yes, assigning numeric values that match EVENT would be a good idea, necessary for sorting in the proper order.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And yes again, UPDATE ignores missing values, but replaces the current value with any nonmissing values it encounters.&amp;nbsp; That's what makes the order essential.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Whatever appears in the BY statement,&amp;nbsp; you will automatically get one observation for each (in this case one observation for each ID).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The obs=1 is a tricky concoction.&amp;nbsp; UPDATE requires two data sets.&amp;nbsp; So using obs=1 creates a second version of the data to use (so that UPDATE won't complain about having just one data set).&amp;nbsp; There is only one observation brought in from that obs=1 data set, and it is immediately overwritten by an observation from the complete HAVE data set.&amp;nbsp; It looks like you are well on your way to understanding the UPDATE process, so don't get bogged down in the tricky aspects at this point.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In all likelihood, this program eliminates the need to process intermediate data sets by dropping variables that are always missing.&amp;nbsp; If it's desirable, you could apply that logic to the final data set.&lt;/P&gt;</description>
      <pubDate>Mon, 15 Feb 2021 19:41:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Accept-first-value-when-merging-datasets/m-p/719462#M9751</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2021-02-15T19:41:37Z</dc:date>
    </item>
  </channel>
</rss>

