<?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: Macro to create new variables with latest values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-new-variables-with-latest-values/m-p/868333#M343013</link>
    <description>I added the following and didn't work.&lt;BR /&gt;data latest_values;&lt;BR /&gt;update &amp;amp;dsin(obs=0) &amp;amp;dsin (keep=bp weight);&lt;BR /&gt;by id;&lt;BR /&gt;ERROR: BY variable ID is not on input data set test.&lt;BR /&gt;ERROR: UPDATE statement needs a BY statement.&lt;BR /&gt;run;</description>
    <pubDate>Wed, 05 Apr 2023 19:15:18 GMT</pubDate>
    <dc:creator>ANKH1</dc:creator>
    <dc:date>2023-04-05T19:15:18Z</dc:date>
    <item>
      <title>Macro to create new variables with latest values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-new-variables-with-latest-values/m-p/868080#M342866</link>
      <description>&lt;P&gt;With the following dataset, how do you code a macro that will create variables per ID that reports the values for each variable (bp, weight, bc) that corresponds to the last form_number? And keep the original variable in the dataset? The number of forms per ID may vary.&lt;/P&gt;&lt;P&gt;data test,&lt;BR /&gt;input ID$ bp weight bc form_number;&lt;BR /&gt;datalines;&lt;BR /&gt;1 120 78 178 1&lt;BR /&gt;1 134 80 177 2&lt;BR /&gt;1 143 . 176 3&lt;BR /&gt;2 111 58 . 1&lt;BR /&gt;3 154 55 160 1&lt;BR /&gt;3 178 56 144 2&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;what we need is the following new dataset:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;bp&lt;/TD&gt;&lt;TD&gt;weight&lt;/TD&gt;&lt;TD&gt;bc&lt;/TD&gt;&lt;TD&gt;form_number&lt;/TD&gt;&lt;TD&gt;late_bp&lt;/TD&gt;&lt;TD&gt;late_weight&lt;/TD&gt;&lt;TD&gt;late_bc&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;120&lt;/TD&gt;&lt;TD&gt;78&lt;/TD&gt;&lt;TD&gt;178&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;143&lt;/TD&gt;&lt;TD&gt;80&lt;/TD&gt;&lt;TD&gt;176&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;134&lt;/TD&gt;&lt;TD&gt;80&lt;/TD&gt;&lt;TD&gt;177&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;143&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;176&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;111&lt;/TD&gt;&lt;TD&gt;58&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;111&lt;/TD&gt;&lt;TD&gt;58&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;154&lt;/TD&gt;&lt;TD&gt;55&lt;/TD&gt;&lt;TD&gt;160&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;178&lt;/TD&gt;&lt;TD&gt;56&lt;/TD&gt;&lt;TD&gt;144&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;178&lt;/TD&gt;&lt;TD&gt;56&lt;/TD&gt;&lt;TD&gt;144&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Apr 2023 20:36:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-new-variables-with-latest-values/m-p/868080#M342866</guid>
      <dc:creator>ANKH1</dc:creator>
      <dc:date>2023-04-04T20:36:48Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to create new variables with latest values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-new-variables-with-latest-values/m-p/868084#M342870</link>
      <description>&lt;P&gt;This is doable, but it's an unusual structure for data.&amp;nbsp; Is there&amp;nbsp; a particular reason you want this format? What's the next step?&amp;nbsp; If you want to calculate, for example, difference between form 1 value and the last value, you could do that without creating these new variables.&lt;/P&gt;</description>
      <pubDate>Tue, 04 Apr 2023 21:04:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-new-variables-with-latest-values/m-p/868084#M342870</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-04-04T21:04:22Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to create new variables with latest values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-new-variables-with-latest-values/m-p/868085#M342871</link>
      <description>&lt;P&gt;I think what you are looking for is a macro that gets the last value for each variable per ID. So something like the below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro get_last(var=);&lt;/P&gt;&lt;P&gt;proc sort data=test; by id; run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data last_&amp;amp;var.;&lt;/P&gt;&lt;P&gt;&amp;nbsp; by id;&lt;/P&gt;&lt;P&gt;&amp;nbsp; set test;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if &amp;amp;var. ne .;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if last.id;&lt;/P&gt;&lt;P&gt;&amp;nbsp; form_number=1;&lt;/P&gt;&lt;P&gt;&amp;nbsp; last_&amp;amp;var.=&amp;amp;var.;&lt;/P&gt;&lt;P&gt;&amp;nbsp; keep id form_number last_&amp;amp;var.;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;%mend get_last;&lt;/P&gt;&lt;P&gt;%get_last(var=bp)&lt;/P&gt;&lt;P&gt;%get_last(var=weight)&lt;/P&gt;&lt;P&gt;%get_last(var=bc)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Afterwards you can merge these datasets back into the original TEST by ID FORM_NUMBER. Note: I'm not sure why there is more than 1 FORM_NUMBER. There can only a single last value. Are FORM_NUMBER 2,3,etc meant to be blank in the final dataset?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Apr 2023 21:06:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-new-variables-with-latest-values/m-p/868085#M342871</guid>
      <dc:creator>Seadrago</dc:creator>
      <dc:date>2023-04-04T21:06:35Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to create new variables with latest values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-new-variables-with-latest-values/m-p/868087#M342873</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/226521"&gt;@ANKH1&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;With the following dataset, how do you code a macro that will create variables per ID that reports the values for each variable (bp, weight, bc) that corresponds to the last form_number? And keep the original variable in the dataset? The number of forms per ID may vary.&lt;/P&gt;
&lt;P&gt;data test,&lt;BR /&gt;input ID$ bp weight bc form_number;&lt;BR /&gt;datalines;&lt;BR /&gt;1 120 78 178 1&lt;BR /&gt;1 134 80 177 2&lt;BR /&gt;1 143 . 176 3&lt;BR /&gt;2 111 58 . 1&lt;BR /&gt;3 154 55 160 1&lt;BR /&gt;3 178 56 144 2&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;what we need is the following new dataset:&lt;/P&gt;
&lt;TABLE&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD&gt;ID&lt;/TD&gt;
&lt;TD&gt;bp&lt;/TD&gt;
&lt;TD&gt;weight&lt;/TD&gt;
&lt;TD&gt;bc&lt;/TD&gt;
&lt;TD&gt;form_number&lt;/TD&gt;
&lt;TD&gt;late_bp&lt;/TD&gt;
&lt;TD&gt;late_weight&lt;/TD&gt;
&lt;TD&gt;late_bc&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;120&lt;/TD&gt;
&lt;TD&gt;78&lt;/TD&gt;
&lt;TD&gt;178&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;143&lt;/TD&gt;
&lt;TD&gt;80&lt;/TD&gt;
&lt;TD&gt;176&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;134&lt;/TD&gt;
&lt;TD&gt;80&lt;/TD&gt;
&lt;TD&gt;177&lt;/TD&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;.&lt;/TD&gt;
&lt;TD&gt;.&lt;/TD&gt;
&lt;TD&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;143&lt;/TD&gt;
&lt;TD&gt;.&lt;/TD&gt;
&lt;TD&gt;176&lt;/TD&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD&gt;.&lt;/TD&gt;
&lt;TD&gt;.&lt;/TD&gt;
&lt;TD&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;111&lt;/TD&gt;
&lt;TD&gt;58&lt;/TD&gt;
&lt;TD&gt;.&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;111&lt;/TD&gt;
&lt;TD&gt;58&lt;/TD&gt;
&lt;TD&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD&gt;154&lt;/TD&gt;
&lt;TD&gt;55&lt;/TD&gt;
&lt;TD&gt;160&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;178&lt;/TD&gt;
&lt;TD&gt;56&lt;/TD&gt;
&lt;TD&gt;144&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD&gt;178&lt;/TD&gt;
&lt;TD&gt;56&lt;/TD&gt;
&lt;TD&gt;144&lt;/TD&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;.&lt;/TD&gt;
&lt;TD&gt;.&lt;/TD&gt;
&lt;TD&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You example &lt;STRONG&gt;does not report the values of the last form number as stated.&lt;/STRONG&gt; You are showing the value of which ever largest form that has a non-missing value, if any. The 80 for Late_weight for the Id=1 is not from the largest form number 3, but from 2.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So please clarify the statement of your need OR make the example match the stated objective.&lt;/P&gt;</description>
      <pubDate>Tue, 04 Apr 2023 21:16:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-new-variables-with-latest-values/m-p/868087#M342873</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-04-04T21:16:19Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to create new variables with latest values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-new-variables-with-latest-values/m-p/868092#M342878</link>
      <description>Need to create a dataset that reports the last non-missing value per variable per ID. Thanks!</description>
      <pubDate>Tue, 04 Apr 2023 21:22:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-new-variables-with-latest-values/m-p/868092#M342878</guid>
      <dc:creator>ANKH1</dc:creator>
      <dc:date>2023-04-04T21:22:02Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to create new variables with latest values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-new-variables-with-latest-values/m-p/868094#M342880</link>
      <description>Sorry, each late_var should report the last non-missing value from the all form_numbers per ID.</description>
      <pubDate>Tue, 04 Apr 2023 21:24:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-new-variables-with-latest-values/m-p/868094#M342880</guid>
      <dc:creator>ANKH1</dc:creator>
      <dc:date>2023-04-04T21:24:37Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to create new variables with latest values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-new-variables-with-latest-values/m-p/868095#M342881</link>
      <description>You are right, it's the last non-missing value per variable. Apologies.</description>
      <pubDate>Tue, 04 Apr 2023 21:25:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-new-variables-with-latest-values/m-p/868095#M342881</guid>
      <dc:creator>ANKH1</dc:creator>
      <dc:date>2023-04-04T21:25:08Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to create new variables with latest values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-new-variables-with-latest-values/m-p/868098#M342883</link>
      <description>and for the late_var columns there should be one value. I am not sure if it is ok to fill all rows corresponding to the same ID filled out with the same value? Example: for ID 1 the late_bp=143 will be repeated 3 times since this ID has three rows.</description>
      <pubDate>Tue, 04 Apr 2023 21:44:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-new-variables-with-latest-values/m-p/868098#M342883</guid>
      <dc:creator>ANKH1</dc:creator>
      <dc:date>2023-04-04T21:44:23Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to create new variables with latest values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-new-variables-with-latest-values/m-p/868099#M342884</link>
      <description>&lt;P&gt;Do you really want macro's here? Why?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Arrays are a better idea, and you could automate the naming of the variables if that's the issue.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data last;
set test;
by ID;

array vars(4) bp weight bc form_number;
array last_vars(4) last_bp last_weight last_bc last_form_number;
retain last_bp last_weight last_bc last_form_number;

if first.id then call missing(of last_vars(*));

do i=1 to dim(vars);
if not missing(vars(i)) then last_vars(i) = vars(i);
end;

if last.id then output;

keep id last_:;
run;

data want;
merge test last;
by id;
if not first.id then call missing(of last:);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 04 Apr 2023 21:47:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-new-variables-with-latest-values/m-p/868099#M342884</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-04-04T21:47:29Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to create new variables with latest values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-new-variables-with-latest-values/m-p/868100#M342885</link>
      <description>&lt;P&gt;Thanks! The reason macros are preferred it's because there are many datasets that we want to run.&lt;/P&gt;</description>
      <pubDate>Tue, 04 Apr 2023 21:49:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-new-variables-with-latest-values/m-p/868100#M342885</guid>
      <dc:creator>ANKH1</dc:creator>
      <dc:date>2023-04-04T21:49:33Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to create new variables with latest values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-new-variables-with-latest-values/m-p/868101#M342886</link>
      <description>Well then you definitely need to specify more details. The macro shown here won't really help with many datasets as its designed for multiple variables not multiple data sets. Do the data sets all have the same names? This is kind of a weird structure, would you not want the last on all rows, that's way more common. Or just create a data set with just the last values for other calculations? I suspect there's something missing in the requirements here.</description>
      <pubDate>Tue, 04 Apr 2023 21:52:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-new-variables-with-latest-values/m-p/868101#M342886</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-04-04T21:52:27Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to create new variables with latest values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-new-variables-with-latest-values/m-p/868103#M342888</link>
      <description>not all datasets have the same variables. the only common variables are ID, form_number. The number of datasets are more than 50. I don't understand why this is considered a weird dataset structure. Datasets collect different info from each ID and the number of forms per ID is not constant across datasets either. Since each dataset asks about different topics.</description>
      <pubDate>Tue, 04 Apr 2023 22:02:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-new-variables-with-latest-values/m-p/868103#M342888</guid>
      <dc:creator>ANKH1</dc:creator>
      <dc:date>2023-04-04T22:02:40Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to create new variables with latest values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-new-variables-with-latest-values/m-p/868105#M342889</link>
      <description>&lt;P&gt;For most of this, macros are not really needed.&amp;nbsp; This is a program that automatically adapts to the variables you have, except for determining names for the new variables.&amp;nbsp; That part you have to add yourself.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   do until (last.id);
      update have (obs=0) have;
      by id;
   end;
   *** The next 3 lines are being hard coded.  If you are clever, it can be automated;
   late_bp = bp;
   late_weight = weight;
   late_bc = bc;
   do until (last.id);
      set have;
      by id;
      output;&lt;BR /&gt;      call missing (of late_:);
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The code is untested, so post again if something isn't working for you.&lt;/P&gt;</description>
      <pubDate>Tue, 04 Apr 2023 22:19:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-new-variables-with-latest-values/m-p/868105#M342889</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2023-04-04T22:19:45Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to create new variables with latest values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-new-variables-with-latest-values/m-p/868107#M342891</link>
      <description>It's an uncommon data structure in that you add the latest to the first row of each ID only. &lt;BR /&gt;&lt;BR /&gt;Whenever I've seen this added it's been added to all rows for that ID or kept in a separate table.</description>
      <pubDate>Tue, 04 Apr 2023 22:37:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-new-variables-with-latest-values/m-p/868107#M342891</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-04-04T22:37:03Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to create new variables with latest values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-new-variables-with-latest-values/m-p/868108#M342892</link>
      <description>&lt;P&gt;Are there other variables in the data set that you would not get the last value for besides ID?&lt;/P&gt;</description>
      <pubDate>Tue, 04 Apr 2023 22:37:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-new-variables-with-latest-values/m-p/868108#M342892</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-04-04T22:37:40Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to create new variables with latest values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-new-variables-with-latest-values/m-p/868110#M342893</link>
      <description>&lt;P&gt;If you want the last non-missing value per variable then treat the whole dataset as a series of transactions by using the UPDATE statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  input ID $ bp weight bc form_number;
datalines;
1 120 78 178 1
1 134 80 177 2
1 143 . 176 3
2 111 58 . 1
3 154 55 160 1
3 178 56 144 2
;

data want;
  update test(obs=0) test;
  by id;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Which would result in:&lt;/P&gt;
&lt;PRE&gt;                                     form_
Obs    ID     bp    weight     bc    number

 1     1     143      80      176       3
 2     2     111      58                1
 3     3     178      56      144       2
&lt;/PRE&gt;
&lt;P&gt;If you need to do it for multiple datasets then get the list of datasets into a dataset and use that to generate the code.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data datasets;
   input input :$41. output :$41.;
cards;
work.test work.want
;

filename code temp;
data _null_;
  set datasets;
  file code;
  put 'data ' output ';' / '  update ' input '(obs=0) ' input ';' / '  by id;' / 'run;' ;
run;
%include code / source2;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 04 Apr 2023 23:23:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-new-variables-with-latest-values/m-p/868110#M342893</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-04-04T23:23:00Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to create new variables with latest values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-new-variables-with-latest-values/m-p/868227#M342951</link>
      <description>&lt;P&gt;I like&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;'s&amp;nbsp;&amp;nbsp;approach (and wish I could write code like that without testing it : ).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As mentioned, the block of code to create the late_ variables could be automated with a macro.&amp;nbsp; Since the variable names you want to LOCF will differ, it's helpful if you have a utility macro that will give you a list of the variables in a dataset, something like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro VarList(data) ;
  %local rc varnames ;
  %let rc = %sysfunc(dosubl(%nrstr(
    proc contents data=&amp;amp;data
      out=__VarList(keep=name) noprint ;
    run ;
    proc sql noprint ;
      select name into :varlist separated by ' '
      from __VarList ;   
      drop table __VarList ;
    quit ;
  )));

&amp;amp;varlist /*return*/
%mend ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then you can use that macro to build a list of the variables that need to be locf'd.&amp;nbsp; In below macro, instead of generating an assignment statement to create each last_ variable, I used a RENAME option on the UPDATE statement.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro locf(data=
           ,out=
           ,by=id
           ,nolocf=form_number
           ) ;

  %local
    varlist    /*list of variables in data that will be locf*/
    renamelist /*list of renames to add last_ prefix */
    i
    vari
  ;

  %*build rename list ;
  %let varlist=%varlist(&amp;amp;data(drop=&amp;amp;by &amp;amp;nolocf)) ;

  %do i=1 %to %sysfunc(countw(&amp;amp;varlist,%str( ))) ;
    %let vari=%scan(&amp;amp;varlist,&amp;amp;i,%str( )) ;
    %let renamelist=&amp;amp;renamelist &amp;amp;vari=last_&amp;amp;vari  ;
  %end ;
  
  %*put &amp;amp;=varlist &amp;amp;=renamelist ;

  data &amp;amp;out;
     do until (last.%sysfunc(scan(&amp;amp;by,-1,%str( ))));
        update &amp;amp;data (rename=(&amp;amp;renamelist) obs=0) &amp;amp;data(rename=(&amp;amp;renamelist) );
        by &amp;amp;by;
     end;

     do until (last.%sysfunc(scan(&amp;amp;by,-1,%str( ))));
        set &amp;amp;data;
        by &amp;amp;by;
        output; 
        call missing (of last_:);
     end;
  run;
%mend locf ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Test like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
input ID$ bp weight bc form_number;
datalines;
1 120 78 178 1
1 134 80 177 2
1 143 .  176 3
2 111 58 .   1
3 154 55 160 1
3 178 56 144 2
;
run;

%locf(data=have,out=want)

proc print data=want ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That said, I agree with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;also.&amp;nbsp; The structure of the input data is not weird.&amp;nbsp; It's great that the input data is in a vertical format.&amp;nbsp; The output structure you want is unusual, where you essentially merge the results of LOCF back onto the data but only for the first record of each ID.&lt;/P&gt;</description>
      <pubDate>Wed, 05 Apr 2023 13:13:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-new-variables-with-latest-values/m-p/868227#M342951</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-04-05T13:13:26Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to create new variables with latest values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-new-variables-with-latest-values/m-p/868243#M342962</link>
      <description>Ok, I misunderstood. The adding of the latest to the first row of each ID only is not a requirement. We can add the value to all rows. Thanks.</description>
      <pubDate>Wed, 05 Apr 2023 13:43:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-new-variables-with-latest-values/m-p/868243#M342962</guid>
      <dc:creator>ANKH1</dc:creator>
      <dc:date>2023-04-05T13:43:40Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to create new variables with latest values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-new-variables-with-latest-values/m-p/868244#M342963</link>
      <description>Yes, the last value is required for a selection of variables per dataset.</description>
      <pubDate>Wed, 05 Apr 2023 13:45:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-new-variables-with-latest-values/m-p/868244#M342963</guid>
      <dc:creator>ANKH1</dc:creator>
      <dc:date>2023-04-05T13:45:23Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to create new variables with latest values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-new-variables-with-latest-values/m-p/868255#M342969</link>
      <description>&lt;P&gt;This is how I would do it, less reading of datasets.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
input ID $ bp weight bc form_number;
datalines;
1 120 78 178 1
1 134 80 177 2
1 143 . 176 3
2 111 58 . 1
3 154 55 160 1
3 178 56 144 2
;
run;

%macro locf(dsin=, dsout=);

*get the latest value;
data latest_values;
update &amp;amp;dsin(obs=0) &amp;amp;dsin;
by id;
run;

*build a rename list for variable names;
proc sql noprint;
select catx("=", name, catt('LAST_', upper(name))) 
into :rename_list
separated by " "
from sashelp.vcolumn
where libname='WORK'
and memname='LATEST_VALUES'
and upper(trim(name)) ne 'ID';
quit;

*merge in final results;
data &amp;amp;dsout;
merge &amp;amp;dsin latest_values (rename = (&amp;amp;rename_list));
by id;
run;

*remove latest values dataset for clean process;
proc sql;
drop table latest_values;
quit;

%mend;

%locf(dsin=test, dsout=want);
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 05 Apr 2023 14:12:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-new-variables-with-latest-values/m-p/868255#M342969</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-04-05T14:12:28Z</dc:date>
    </item>
  </channel>
</rss>

