<?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 Choose last non-missing observation for multiple variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Choose-last-non-missing-observation-for-multiple-variables/m-p/209270#M306810</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have a dataset where participants have up to 4 visits. I need to select their most recent visit for multiple variables that does not include missing values. I know I can use last.studyid to select their most recent observation but is there a way for multiple variables to select their most recent observation for each variable that does not include missing values? A short example of what the data might look like it below. Bascially for var1 I would want the third visit value for var1 and the fourth visit value for var2 for study ID 1, for study id 2 I would want the third visit value for both, and for study ID 3 I would want the fourth visit value for var1 and the third visit value for var2. Any help would be great appreciated.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;ID var1 var2&amp;nbsp; visit&lt;/P&gt;&lt;P&gt;1&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;/P&gt;&lt;P&gt;1&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&lt;/P&gt;&lt;P&gt;1&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3&lt;/P&gt;&lt;P&gt;1&amp;nbsp;&amp;nbsp; .&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0&amp;nbsp;&amp;nbsp;&amp;nbsp; 4&lt;/P&gt;&lt;P&gt;2&amp;nbsp;&amp;nbsp; .&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;/P&gt;&lt;P&gt;2&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&lt;/P&gt;&lt;P&gt;2&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp; 3&lt;/P&gt;&lt;P&gt;3&amp;nbsp; 0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;/P&gt;&lt;P&gt;3&amp;nbsp; 0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&lt;/P&gt;&lt;P&gt;3&amp;nbsp; 0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp; 3&lt;/P&gt;&lt;P&gt;3&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .&amp;nbsp;&amp;nbsp;&amp;nbsp; 4&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 18 Jun 2015 15:32:41 GMT</pubDate>
    <dc:creator>rfarmenta</dc:creator>
    <dc:date>2015-06-18T15:32:41Z</dc:date>
    <item>
      <title>Choose last non-missing observation for multiple variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Choose-last-non-missing-observation-for-multiple-variables/m-p/209270#M306810</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have a dataset where participants have up to 4 visits. I need to select their most recent visit for multiple variables that does not include missing values. I know I can use last.studyid to select their most recent observation but is there a way for multiple variables to select their most recent observation for each variable that does not include missing values? A short example of what the data might look like it below. Bascially for var1 I would want the third visit value for var1 and the fourth visit value for var2 for study ID 1, for study id 2 I would want the third visit value for both, and for study ID 3 I would want the fourth visit value for var1 and the third visit value for var2. Any help would be great appreciated.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;ID var1 var2&amp;nbsp; visit&lt;/P&gt;&lt;P&gt;1&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;/P&gt;&lt;P&gt;1&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&lt;/P&gt;&lt;P&gt;1&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3&lt;/P&gt;&lt;P&gt;1&amp;nbsp;&amp;nbsp; .&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0&amp;nbsp;&amp;nbsp;&amp;nbsp; 4&lt;/P&gt;&lt;P&gt;2&amp;nbsp;&amp;nbsp; .&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;/P&gt;&lt;P&gt;2&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&lt;/P&gt;&lt;P&gt;2&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp; 3&lt;/P&gt;&lt;P&gt;3&amp;nbsp; 0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;/P&gt;&lt;P&gt;3&amp;nbsp; 0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&lt;/P&gt;&lt;P&gt;3&amp;nbsp; 0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp; 3&lt;/P&gt;&lt;P&gt;3&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .&amp;nbsp;&amp;nbsp;&amp;nbsp; 4&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 18 Jun 2015 15:32:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Choose-last-non-missing-observation-for-multiple-variables/m-p/209270#M306810</guid>
      <dc:creator>rfarmenta</dc:creator>
      <dc:date>2015-06-18T15:32:41Z</dc:date>
    </item>
    <item>
      <title>Re: Choose last non-missing observation for multiple variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Choose-last-non-missing-observation-for-multiple-variables/m-p/209271#M306811</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;This will get the result you want but you only see the last visit, not where the info comes from.&amp;nbsp; Let me know if this helps:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data have;&lt;/P&gt;&lt;P&gt;input ID$ var1 var2&amp;nbsp; visit;&lt;/P&gt;&lt;P&gt;cards;&lt;/P&gt;&lt;P&gt;1&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;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t;/P&gt;&lt;P&gt;;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;set have;&lt;/P&gt;&lt;P&gt;by id visit;&lt;/P&gt;&lt;P&gt;retain _newvar1 _newvar2;&lt;/P&gt;&lt;P&gt;if first.id then do;&lt;/P&gt;&lt;P&gt;_newvar1 = var1;&lt;/P&gt;&lt;P&gt;_newvar2 = var2;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;if missing(var1) then var1 = _newvar1;&lt;/P&gt;&lt;P&gt;if missing(var2) then var2 = _newvar2;&lt;/P&gt;&lt;P&gt;drop _:;&lt;/P&gt;&lt;P&gt;if last.id then output;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 18 Jun 2015 15:57:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Choose-last-non-missing-observation-for-multiple-variables/m-p/209271#M306811</guid>
      <dc:creator>Steelers_In_DC</dc:creator>
      <dc:date>2015-06-18T15:57:47Z</dc:date>
    </item>
    <item>
      <title>Re: Choose last non-missing observation for multiple variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Choose-last-non-missing-observation-for-multiple-variables/m-p/209272#M306812</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;A simple DATA step tool can make this easy:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; update have (obs=0) have;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; by id;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; drop visit;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You don't need to know the variable names other than ID and VISIT.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 18 Jun 2015 16:24:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Choose-last-non-missing-observation-for-multiple-variables/m-p/209272#M306812</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2015-06-18T16:24:11Z</dc:date>
    </item>
    <item>
      <title>Re: Choose last non-missing observation for multiple variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Choose-last-non-missing-observation-for-multiple-variables/m-p/209273#M306813</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you both for your replies. Astounding-Can you give me a little more information as to what the obs=0 does in this case? From reading the SAS documentation it tells SAS when to stop processing. So in this case, if a missing value is set to obs=0 then will it choose the previous value? Also, why would I drop visit?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Mark-I am going to try your suggestion and see if that works as well. In your case it appears as though I will fill the missing with the first observations value, what if that is missing as well?&amp;nbsp; Thanks! &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 18 Jun 2015 16:50:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Choose-last-non-missing-observation-for-multiple-variables/m-p/209273#M306813</guid>
      <dc:creator>rfarmenta</dc:creator>
      <dc:date>2015-06-18T16:50:22Z</dc:date>
    </item>
    <item>
      <title>Re: Choose last non-missing observation for multiple variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Choose-last-non-missing-observation-for-multiple-variables/m-p/209274#M306814</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;VISIT does not have to be dropped ... but there will only be one observation per ID in the output.&amp;nbsp; So VISIT will represent the last VISIT value for that ID (whether or not any other variables were actually non-missing on that visit).&amp;nbsp; If that would be helpful, keep it.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;UPDATE requires two data sets ... a master (with no more than one observation per ID) and a set of transactions (with potentially many observations per ID).&amp;nbsp; So the first mention of HAVE with OBS=0 is just to satisfy the requirements of the UPDATE statement.&amp;nbsp; It doesn't bring in any variable values, but it does let UPDATE process the second mention of HAVE.&amp;nbsp; UPDATE applies all of the transactions, ignores missing values, and automatically outputs a single observation per ID.&amp;nbsp; In this case, that's just what the doctor ordered.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 18 Jun 2015 17:02:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Choose-last-non-missing-observation-for-multiple-variables/m-p/209274#M306814</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2015-06-18T17:02:35Z</dc:date>
    </item>
    <item>
      <title>Re: Choose last non-missing observation for multiple variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Choose-last-non-missing-observation-for-multiple-variables/m-p/209275#M306815</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Comment out the drop _:; line and you'll see what you are getting.&amp;nbsp;&amp;nbsp; You aren't getting the first value but the last value that is not missing.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 18 Jun 2015 17:47:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Choose-last-non-missing-observation-for-multiple-variables/m-p/209275#M306815</guid>
      <dc:creator>Steelers_In_DC</dc:creator>
      <dc:date>2015-06-18T17:47:52Z</dc:date>
    </item>
    <item>
      <title>Re: Choose last non-missing observation for multiple variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Choose-last-non-missing-observation-for-multiple-variables/m-p/209276#M306816</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you both for your help. They both seem to have worked and give me the same results. &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 18 Jun 2015 23:13:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Choose-last-non-missing-observation-for-multiple-variables/m-p/209276#M306816</guid>
      <dc:creator>rfarmenta</dc:creator>
      <dc:date>2015-06-18T23:13:02Z</dc:date>
    </item>
  </channel>
</rss>

