<?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 in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Run-a-macro-on-all-columns/m-p/798481#M313904</link>
    <description>&lt;P&gt;You are just trying to add an ascending DATE variable?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data add_date;
  format date yymmdd10.;
  set have nobs=nobs;
  date=today()-nobs+n;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;Obs          date    n    v1    v2    v3

 1     2022-02-19    1    33    44     1
 2     2022-02-20    2     5     6     5
 3     2022-02-21    3     4     5     7
 4     2022-02-22    4     .     7     .
 5     2022-02-23    5    66     4     .
 6     2022-02-24    6     .     9     .

&lt;/PRE&gt;</description>
    <pubDate>Fri, 25 Feb 2022 00:33:03 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2022-02-25T00:33:03Z</dc:date>
    <item>
      <title>Run a macro on all columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Run-a-macro-on-all-columns/m-p/798478#M313901</link>
      <description>&lt;P&gt;Hi Everyone,&lt;/P&gt;
&lt;P&gt;My data has a 1000+ columns with data in the first row. The &lt;U&gt;&lt;STRONG&gt;last&lt;/STRONG&gt; &lt;/U&gt;available data is data of today (02JAN2022).&lt;/P&gt;
&lt;P&gt;So I want to fill the date backward for each row.&lt;/P&gt;
&lt;P&gt;Ok, the Macro as shown below produce what I want. However I need 1 line of macro for each variable/column. (This macro is not great yet, I am still working on it).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Clearly, I need to loop through all columns in my data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&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;P&gt;SOLUTION:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have; 
  input n v1-v3;
datalines;
1 33 44 1
2 5 6 5
3 4 5 7
4 . 7 .
5 66 4 .
6 . 9 .
;

data have; set have;
date_id=_N_;run;
proc sort data=have; by descending date_id;run;

proc transpose data=have out=want ;
  by descending date_id ;
  var v1-v3;
run;


proc sort data=want; by _NAME_ descending date_id; run;

*Delete all missing row UP TO the first data available;
data want2(drop=_d:);
  set want;
  retain  _del_flg 1;
  by _NAME_;
  if _del_flg or first._NAME_ then 
    do;
      _del_flg=missing(col1);
      if _del_flg then delete;
    end;	run;

*find max date-id for each id;
proc means data=want2 noprint;
by _NAME_;
var date_id;
output out=maxdate Max=Maxdate; run;

proc sql; create table want2 as select a.* , Maxdate from want2 a left join maxdate b on a._NAME_=b._NAME_
order by _NAME_, date_id desc; quit;

data want2; set want2;
format date date9.;
date = today() - (Maxdate - date_id) -1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have; 
input n v1 v2 v3;
datalines;
1 33 44 1
2 5 6 5
3 4 5 7
4 . 7 .
5 66 4 .
6 . 9 .
;run;&lt;BR /&gt;
%let TODAY= '02JAN2022'd;&lt;BR /&gt;
data output; set _NULL_;run;

%MACRO lastdate( column=);
data subfile; set have;
keep n &amp;amp;column;run;

proc sort data=subfile; by descending n;run;

data first_not_missing; set subfile;
if &amp;amp;column^=.;run;

data first_not_missing; set first_not_missing;
rename n=first_not_missing;
keep n;
if _N_=1;run;

proc sql;
create table subfile as select a.* from subfile a left join first_not_missing on 1=1 WHERE n&amp;lt;=first_not_missing
order by n desc;quit;

data lastdate; set subfile; 
keep n; rename n=lastdate;
if _N_=1;run;

proc sql;
create table subfile as select * from subfile left join lastdate on 1=1;quit;

data subfile; length variable_name $8.; set subfile;
format datadate date10.;
rename &amp;amp;column=value;
keep &amp;amp;column datadate variable_name;
datadate=&amp;amp;today - (lastdate - n);
variable_name=vname(&amp;amp;column);
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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Mar 2022 18:13:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Run-a-macro-on-all-columns/m-p/798478#M313901</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2022-03-15T18:13:40Z</dc:date>
    </item>
    <item>
      <title>Re: Run a macro on all columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Run-a-macro-on-all-columns/m-p/798480#M313903</link>
      <description>&lt;P&gt;Can you please explain WHAT you are trying to accomplish?&lt;/P&gt;
&lt;P&gt;What does "fill the date backward" mean?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I cannot figure out what the heck that macro is trying to do.&lt;/P&gt;
&lt;P&gt;What is the output you want from your example input data?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Feb 2022 00:20:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Run-a-macro-on-all-columns/m-p/798480#M313903</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-02-25T00:20:57Z</dc:date>
    </item>
    <item>
      <title>Re: Run a macro on all columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Run-a-macro-on-all-columns/m-p/798481#M313904</link>
      <description>&lt;P&gt;You are just trying to add an ascending DATE variable?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data add_date;
  format date yymmdd10.;
  set have nobs=nobs;
  date=today()-nobs+n;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;Obs          date    n    v1    v2    v3

 1     2022-02-19    1    33    44     1
 2     2022-02-20    2     5     6     5
 3     2022-02-21    3     4     5     7
 4     2022-02-22    4     .     7     .
 5     2022-02-23    5    66     4     .
 6     2022-02-24    6     .     9     .

&lt;/PRE&gt;</description>
      <pubDate>Fri, 25 Feb 2022 00:33:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Run-a-macro-on-all-columns/m-p/798481#M313904</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-02-25T00:33:03Z</dc:date>
    </item>
    <item>
      <title>Re: Run a macro on all columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Run-a-macro-on-all-columns/m-p/798483#M313906</link>
      <description>&lt;P&gt;Hi Tom,&lt;/P&gt;
&lt;P&gt;The output I want is produced by this macro.&lt;/P&gt;
&lt;P&gt;The last date is 02JAN2022&lt;/P&gt;
&lt;P&gt;I am still working on making the macro efficient&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Obs variable_name value datadate 
1 v1 66 02JAN2022 
2 v1 . 01JAN2022 
3 v1 4 31DEC2021 
4 v1 5 30DEC2021 
5 v1 33 29DEC2021 &lt;BR /&gt;
6 v2 9 02JAN2022 
7 v2 4 01JAN2022 
8 v2 7 31DEC2021 
9 v2 5 30DEC2021 
10 v2 6 29DEC2021 &lt;BR /&gt;
11 v2 44 28DEC2021 
12 v3 7 02JAN2022 
13 v3 5 01JAN2022 
14 v3 1 31DEC2021 
&lt;/CODE&gt;&amp;nbsp;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Feb 2022 00:57:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Run-a-macro-on-all-columns/m-p/798483#M313906</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2022-02-25T00:57:25Z</dc:date>
    </item>
    <item>
      <title>Re: Run a macro on all columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Run-a-macro-on-all-columns/m-p/798485#M313908</link>
      <description>&lt;P&gt;How about providing an example of the expected data set.&lt;/P&gt;
&lt;P&gt;I get lazy this time of day and don't feel like attempting to parse code that the author indicates may not be doing what is needed. (If it did do what was needed would there be a question?)&lt;/P&gt;</description>
      <pubDate>Fri, 25 Feb 2022 00:54:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Run-a-macro-on-all-columns/m-p/798485#M313908</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-02-25T00:54:34Z</dc:date>
    </item>
    <item>
      <title>Re: Run a macro on all columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Run-a-macro-on-all-columns/m-p/798488#M313910</link>
      <description>&lt;P&gt;Why is this not a simple PROC TRANSPOSE application?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have; 
  input n v1-v3;
datalines;
1 33 44 1
2 5 6 5
3 4 5 7
4 . 7 .
5 66 4 .
6 . 9 .
;


data add_date / view=add_date;
  format date yymmdd10.;
  set have nobs=nobs;
  date=today()-nobs+n;
run;

proc transpose data=add_date out=want ;
  by date ;
  var v1-v3;
run;

proc sort;
  by _name_ date;
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt;Obs          date    _NAME_    COL1

  1    2022-02-19      v1       33
  2    2022-02-20      v1        5
  3    2022-02-21      v1        4
  4    2022-02-22      v1        .
  5    2022-02-23      v1       66
  6    2022-02-24      v1        .
  7    2022-02-19      v2       44
  8    2022-02-20      v2        6
  9    2022-02-21      v2        5
 10    2022-02-22      v2        7
 11    2022-02-23      v2        4
 12    2022-02-24      v2        9
 13    2022-02-19      v3        1
 14    2022-02-20      v3        5
 15    2022-02-21      v3        7
 16    2022-02-22      v3        .
 17    2022-02-23      v3        .
 18    2022-02-24      v3        .

&lt;/PRE&gt;</description>
      <pubDate>Fri, 25 Feb 2022 01:04:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Run-a-macro-on-all-columns/m-p/798488#M313910</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-02-25T01:04:07Z</dc:date>
    </item>
    <item>
      <title>Re: Run a macro on all columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Run-a-macro-on-all-columns/m-p/798491#M313913</link>
      <description>&lt;P&gt;I am so sorry for not making thing clear.&lt;/P&gt;
&lt;P&gt;I got my code right and will report.&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;</description>
      <pubDate>Fri, 25 Feb 2022 01:11:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Run-a-macro-on-all-columns/m-p/798491#M313913</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2022-02-25T01:11:06Z</dc:date>
    </item>
    <item>
      <title>Re: Run a macro on all columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Run-a-macro-on-all-columns/m-p/798492#M313914</link>
      <description>&lt;P&gt;Does not appear to be any need to "macro" solution for this problem.&lt;/P&gt;
&lt;P&gt;It should be simple to adjust the PROC TRANSPOSE code to use different names for the variables.&lt;/P&gt;
&lt;P&gt;Easy to adjust the date calculations to use a different ending date.&lt;/P&gt;
&lt;P&gt;You could change the PROC SORT to list the data by descending date if you want.&lt;/P&gt;
&lt;P&gt;You could then add an extra step to remove the trailing (now leading) missing values.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data fixed;
  set want;
  by _name_ descending date ;
  if first._name_ then found =0;
  retain found;
  if not missing(col1) then found=1;
  if found then output;
  drop found;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 25 Feb 2022 01:18:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Run-a-macro-on-all-columns/m-p/798492#M313914</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-02-25T01:18:51Z</dc:date>
    </item>
    <item>
      <title>Re: Run a macro on all columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Run-a-macro-on-all-columns/m-p/798493#M313915</link>
      <description>&lt;P&gt;Assuming your code works and does what you need here's how to use CALL EXECUTE to run it for all variables in your data set. I'm assuming you don't want it to run for the variable N which you didn't state as a requirement but you can change the code as needed.&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;
&lt;LI-SPOILER&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/49486"&gt;@hhchenfx&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi Everyone,&lt;/P&gt;
&lt;P&gt;My data has a 1000+ columns with data in the first row. The &lt;U&gt;&lt;STRONG&gt;last&lt;/STRONG&gt; &lt;/U&gt;available data is data of today (02JAN2022).&lt;/P&gt;
&lt;P&gt;So I want to fill the date backward for each row.&lt;/P&gt;
&lt;P&gt;Ok, the Macro as shown below produce what I want. However I need 1 line of macro for each variable/column. (This macro is not great yet, I am still working on it).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Clearly, I need to loop through all columns in my data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&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;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have; 
input n v1 v2 v3;
datalines;
1 33 44 1
2 5 6 5
3 4 5 7
4 . 7 .
5 66 4 .
6 . 9 .
;run;&lt;BR /&gt;
%let TODAY= '02JAN2022'd;&lt;BR /&gt;
data output; set _NULL_;run;

%MACRO lastdate( column=);
data subfile; set have;
keep n &amp;amp;column;run;

proc sort data=subfile; by descending n;run;

data first_not_missing; set subfile;
if &amp;amp;column^=.;run;

data first_not_missing; set first_not_missing;
rename n=first_not_missing;
keep n;
if _N_=1;run;

proc sql;
create table subfile as select a.* from subfile a left join first_not_missing on 1=1 WHERE n&amp;lt;=first_not_missing
order by n desc;quit;

data lastdate; set subfile; 
keep n; rename n=lastdate;
if _N_=1;run;

proc sql;
create table subfile as select * from subfile left join lastdate on 1=1;quit;

data subfile; length variable_name $8.; set subfile;
format datadate date10.;
rename &amp;amp;column=value;
keep &amp;amp;column datadate variable_name;
datadate=&amp;amp;today - (lastdate - n);
variable_name=vname(&amp;amp;column);
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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;/LI-SPOILER&gt;</description>
      <pubDate>Fri, 25 Feb 2022 01:23:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Run-a-macro-on-all-columns/m-p/798493#M313915</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-02-25T01:23:23Z</dc:date>
    </item>
    <item>
      <title>Re: Run a macro on all columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Run-a-macro-on-all-columns/m-p/802299#M315834</link>
      <description>&lt;P&gt;You are right, Tom.&lt;/P&gt;
&lt;P&gt;I should do transpose and work from there!&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;</description>
      <pubDate>Tue, 15 Mar 2022 17:58:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Run-a-macro-on-all-columns/m-p/802299#M315834</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2022-03-15T17:58:37Z</dc:date>
    </item>
    <item>
      <title>Re: Run a macro on all columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Run-a-macro-on-all-columns/m-p/802307#M315837</link>
      <description>&lt;P&gt;Here is mine final one&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have; 
  input n v1-v3;
datalines;
1 33 44 1
2 5 6 5
3 4 5 7
4 . 7 .
5 66 4 .
6 . 9 .
;

data have; set have;
date_id=_N_;run;
proc sort data=have; by descending date_id;run;

proc transpose data=have out=want ;
  by descending date_id ;
  var v1-v3;
run;


proc sort data=want; by _NAME_ descending date_id; run;

*Delete all missing row UP TO the first data available;
data want2(drop=_d:);
  set want;
  retain  _del_flg 1;
  by _NAME_;
  if _del_flg or first._NAME_ then 
    do;
      _del_flg=missing(col1);
      if _del_flg then delete;
    end;	run;

*find max date-id for each id;
proc means data=want2 noprint;
by _NAME_;
var date_id;
output out=maxdate Max=Maxdate; run;

proc sql; create table want2 as select a.* , Maxdate from want2 a left join maxdate b on a._NAME_=b._NAME_
order by _NAME_, date_id desc; quit;

data want2; set want2;
format date date9.;
date = today() - (Maxdate - date_id) -1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 15 Mar 2022 18:13:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Run-a-macro-on-all-columns/m-p/802307#M315837</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2022-03-15T18:13:01Z</dc:date>
    </item>
  </channel>
</rss>

