<?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: Run a macro on all columns - Repost in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Run-a-macro-on-all-columns-Repost/m-p/798503#M313922</link>
    <description>&lt;P&gt;The macro still looks convoluted and confusing.&lt;/P&gt;
&lt;P&gt;It seems you are trying to do this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro lastdate(column=);
* Sort by descending DATE_ID ;
proc sort data=have(keep=date_id &amp;amp;column rename=(&amp;amp;column=value)) out=subfile;
  by descending date_id;
run;

*Delete all missing row UP TO the first data available which is today;
data subfile;
  length variable_name $8;
  retain variable_name "&amp;amp;column" lastdate;
  set subfile;
  retain _del_flg 1;
  if _del_flg then do;
     if missing(value) then delete;
     lastdate=date_id;
     _del_flg=missing(value);
  end;
  datadate=today() - (lastdate-date_id);
  format datadate date9.;
  keep variable_name datadate value;
run;

*Add to output file;
proc append base=output data=subfile force;
run;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So given a HAVE dataset like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input date_id v1-v3;
datalines;
1 33 44 1
2 5 6 5
3 4 5 7
4 . 7 .
5 66 4 .
6 . 9 .
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want to run it for every variable except DATA_DATE then you could use something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc delete data=output; run;
proc transpose data=have(drop=date_id obs=0) out=names; run;
data _null_;
  set names;
  call execute(cats('%nrstr(%lastdate)(column=',_name_,')'));
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt;       variable_
Obs      name       value     datadate

  1       v1          66     24FEB2022
  2       v1           .     23FEB2022
  3       v1           4     22FEB2022
  4       v1           5     21FEB2022
  5       v1          33     20FEB2022
  6       v2           9     24FEB2022
  7       v2           4     23FEB2022
  8       v2           7     22FEB2022
  9       v2           5     21FEB2022
 10       v2           6     20FEB2022
 11       v2          44     19FEB2022
 12       v3           7     24FEB2022
 13       v3           5     23FEB2022
 14       v3           1     22FEB2022&lt;/PRE&gt;</description>
    <pubDate>Fri, 25 Feb 2022 02:35:48 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2022-02-25T02:35:48Z</dc:date>
    <item>
      <title>Run a macro on all columns - Repost</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Run-a-macro-on-all-columns-Repost/m-p/798494#M313916</link>
      <description>&lt;P&gt;Hi Everyone,&lt;/P&gt;
&lt;P&gt;My data has a 1000+ columns. The &lt;U&gt;&lt;STRONG&gt;LAST available&lt;/STRONG&gt;&lt;/U&gt; data in a column is data of today().(So in the code, I sort descending date_id to bring the today up to top). Note, after the last available data row, there could be no value rows.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Based on the last data is today(), I need to fill date for each row.&lt;/P&gt;
&lt;P&gt;Ok, the Macro as shown below produce the exact output I want.&lt;/P&gt;
&lt;P&gt;However I need 1 line of macro for each variable/column.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Clearly, I need to loop through all columns in my data.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Can you please help me to create a loop code?&lt;/P&gt;
&lt;P&gt;Thank you,&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have; 
input date_id v1 v2 v3;
datalines;
1 33 44 1
2 5 6 5
3 4 5 .
4 . 7 .
5 66 4 6
6 . 9 .
;run;
data output; set _NULL_;run;

%MACRO lastdate( column=);

*create subfile with only: column date_id and 1 variable;
data subfile; set have;
keep date_id &amp;amp;column;&lt;BR /&gt;run;

*sort to bring the most recent date on top;
proc sort data=subfile; by descending date_id;run;

*Delete all missing row UP TO the first data available which is today;
data subfile(drop=_:);
  set subfile;
  retain _del_flg 1;
  if _del_flg then 
    do;
      _del_flg=missing(&amp;amp;column);
      if _del_flg then delete;
    end;
run;

*Fill Date;
data subfile; length variable_name $8.;set subfile;
keep &amp;amp;column variable_name datadate;
rename &amp;amp;column=value;
variable_name=vname(&amp;amp;column);

format datadate date10.;
if _N_=1 then  lastdate=date_id;
retain lastdate;
datadate=today() - (lastdate-date_id);

run;

*Add to output file;
data output; set output subfile; run;
%mend;

%lastdate( column=v1);
%lastdate( column=v2);
%lastdate( column=v3);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 25 Feb 2022 01:23:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Run-a-macro-on-all-columns-Repost/m-p/798494#M313916</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2022-02-25T01:23:25Z</dc:date>
    </item>
    <item>
      <title>Re: Run a macro on all columns - Repost</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Run-a-macro-on-all-columns-Repost/m-p/798495#M313917</link>
      <description>&lt;P&gt;I posted the answer in your previous question.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
ods select none;
proc contents data=have out=var_list (keep = name);
run;
ods select all;

data execute_all;
set var_list;
where name ne 'n';

str = catt('%lastdate(column=', name, ');');
call execute(str);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 25 Feb 2022 01:25:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Run-a-macro-on-all-columns-Repost/m-p/798495#M313917</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-02-25T01:25:08Z</dc:date>
    </item>
    <item>
      <title>Re: Run a macro on all columns - Repost</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Run-a-macro-on-all-columns-Repost/m-p/798496#M313918</link>
      <description>&lt;P&gt;Thank you for helping!&lt;/P&gt;
&lt;P&gt;You save my day.&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;</description>
      <pubDate>Fri, 25 Feb 2022 01:28:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Run-a-macro-on-all-columns-Repost/m-p/798496#M313918</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2022-02-25T01:28:12Z</dc:date>
    </item>
    <item>
      <title>Re: Run a macro on all columns - Repost</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Run-a-macro-on-all-columns-Repost/m-p/798503#M313922</link>
      <description>&lt;P&gt;The macro still looks convoluted and confusing.&lt;/P&gt;
&lt;P&gt;It seems you are trying to do this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro lastdate(column=);
* Sort by descending DATE_ID ;
proc sort data=have(keep=date_id &amp;amp;column rename=(&amp;amp;column=value)) out=subfile;
  by descending date_id;
run;

*Delete all missing row UP TO the first data available which is today;
data subfile;
  length variable_name $8;
  retain variable_name "&amp;amp;column" lastdate;
  set subfile;
  retain _del_flg 1;
  if _del_flg then do;
     if missing(value) then delete;
     lastdate=date_id;
     _del_flg=missing(value);
  end;
  datadate=today() - (lastdate-date_id);
  format datadate date9.;
  keep variable_name datadate value;
run;

*Add to output file;
proc append base=output data=subfile force;
run;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So given a HAVE dataset like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input date_id v1-v3;
datalines;
1 33 44 1
2 5 6 5
3 4 5 7
4 . 7 .
5 66 4 .
6 . 9 .
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want to run it for every variable except DATA_DATE then you could use something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc delete data=output; run;
proc transpose data=have(drop=date_id obs=0) out=names; run;
data _null_;
  set names;
  call execute(cats('%nrstr(%lastdate)(column=',_name_,')'));
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt;       variable_
Obs      name       value     datadate

  1       v1          66     24FEB2022
  2       v1           .     23FEB2022
  3       v1           4     22FEB2022
  4       v1           5     21FEB2022
  5       v1          33     20FEB2022
  6       v2           9     24FEB2022
  7       v2           4     23FEB2022
  8       v2           7     22FEB2022
  9       v2           5     21FEB2022
 10       v2           6     20FEB2022
 11       v2          44     19FEB2022
 12       v3           7     24FEB2022
 13       v3           5     23FEB2022
 14       v3           1     22FEB2022&lt;/PRE&gt;</description>
      <pubDate>Fri, 25 Feb 2022 02:35:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Run-a-macro-on-all-columns-Repost/m-p/798503#M313922</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-02-25T02:35:48Z</dc:date>
    </item>
    <item>
      <title>Re: Run a macro on all columns - Repost</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Run-a-macro-on-all-columns-Repost/m-p/799037#M314148</link>
      <description>&lt;P&gt;Thanks Tom.&lt;/P&gt;
&lt;P&gt;I will try your method.&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;</description>
      <pubDate>Mon, 28 Feb 2022 00:41:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Run-a-macro-on-all-columns-Repost/m-p/799037#M314148</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2022-02-28T00:41:20Z</dc:date>
    </item>
  </channel>
</rss>

