<?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: How to simplify this macro code to calculate mulitple dates from multiple dataset? thanks in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-this-macro-code-to-calculate-mulitple-dates-from/m-p/820530#M323872</link>
    <description>&lt;P&gt;Tom and Kurt: &lt;BR /&gt;That is exactly what I need. thanks all for make the puzzle resolved. super. !&lt;BR /&gt;I need to test the code and let you know.&lt;/P&gt;
&lt;P&gt;thanks again,&lt;/P&gt;
&lt;P&gt;Purple&lt;/P&gt;</description>
    <pubDate>Mon, 27 Jun 2022 13:52:08 GMT</pubDate>
    <dc:creator>purpleclothlady</dc:creator>
    <dc:date>2022-06-27T13:52:08Z</dc:date>
    <item>
      <title>How to simplify this macro code to calculate mulitple dates from multiple dataset? thanks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-this-macro-code-to-calculate-mulitple-dates-from/m-p/820294#M323759</link>
      <description>&lt;P&gt;hi all:&lt;BR /&gt;how may I simplify the code? It works but there might be more efficient way. thanks in advance.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options mprint mlogic;
%macro pd(ord,ds,ovar,fvar,v1,v2,v3,v4);

data &amp;amp;ds (keep=subject &amp;amp;fvar.: ds _lastf);
length ds $10.;
   set new.&amp;amp;ds(keep= subject 
                   %if &amp;amp;ord in (-1,-2) %then %do; &amp;amp;v1.:  %end;
                   %else %if &amp;amp;ord=1    %then %do; &amp;amp;v1.: &amp;amp;v2.: %end;
                   %else %if &amp;amp;ord=2    %then %do; &amp;amp;v1.: &amp;amp;v2.: &amp;amp;v3.: %end;
                   %else %if &amp;amp;ord=3    %then %do; &amp;amp;v1.: &amp;amp;v2.: &amp;amp;v3.: &amp;amp;v4.:%end;
                   );		
		  ds="&amp;amp;ds.";
		 %if &amp;amp;ord=-2 %then %do;
		  _lasf=&amp;amp;fvar.;
	   %end;

	   %if &amp;amp;ord ne -2 %then %do;
			if 	&amp;amp;v1._dd      ^=. then &amp;amp;ovar.=put(&amp;amp;v1._yyyy, 4.)||'-'||put(&amp;amp;v1._mm, z2.)||'-'||put(&amp;amp;v1._dd, z2.);
			else if &amp;amp;v1._mm  ^=. then &amp;amp;ovar.=put(&amp;amp;v1._yyyy, 4.)||'-'||put(&amp;amp;v1._mm, z2.)||'-01';
			else if &amp;amp;v1._yyyy^=. then &amp;amp;ovar.=put(&amp;amp;v1._yyyy, 4.)||'-01'||'-01';
			&amp;amp;fvar.=input(&amp;amp;ovar.,yymmdd10.);	format &amp;amp;fvar. yymmdd10.;
			_lastf=&amp;amp;fvar.;
		 %end;

		 %else %if &amp;amp;ord&amp;gt;=1 %then %do;		 	 
			if 	&amp;amp;v2._dd^=.        then &amp;amp;ovar.2=put(&amp;amp;v2._yyyy, 4.)||'-'||put(&amp;amp;v2._mm, z2.)||'-'||put(&amp;amp;v2._dd, z2.);
			else if &amp;amp;v2._mm^=.    then &amp;amp;ovar.2=put(&amp;amp;v2._yyyy, 4.)||'-'||put(&amp;amp;v2._mm, z2.)||'-01';
			else if &amp;amp;v2._yyyy^=.  then &amp;amp;ovar.2=put(&amp;amp;v2._yyyy, 4.)||'-01'||'-01';
			&amp;amp;fvar.2=input(&amp;amp;ovar.2,yymmdd10.);	format &amp;amp;fvar.2 yymmdd10.;

			 array dt &amp;amp;fvar. &amp;amp;fvar.2;
			  do over dt;
			  if dt=. then dt="01JAN1900"d;
			 end;
			 _lastf=max(of dt[*]);
		%end;
		 %if &amp;amp;ord&amp;gt;=2 %then %do;
			    if 	&amp;amp;v3._dd^=.    then &amp;amp;ovar.3=put(&amp;amp;v3._yyyy, 4.)||'-'||put(&amp;amp;v3._mm, z2.)||'-'||put(&amp;amp;v3._dd, z2.);
			else if &amp;amp;v3._mm^=.    then &amp;amp;ovar.3=put(&amp;amp;v3._yyyy, 4.)||'-'||put(&amp;amp;v3._mm, z2.)||'-01';
			else if &amp;amp;v3._yyyy^=.  then &amp;amp;ovar.3=put(&amp;amp;v3._yyyy, 4.)||'-01'||'-01';
			&amp;amp;fvar.3=input(&amp;amp;ovar.3,yymmdd10.);	format &amp;amp;fvar.3 yymmdd10.;

			 array dt2 &amp;amp;fvar. &amp;amp;fvar.2 &amp;amp;fvar.3;
			  do over dt2;
			  if dt2=. then dt2="01JAN1900"d;
			 end;
			 _lastf=max(of dt2[*]);
		%end;
		%if &amp;amp;ord=3 %then %do;
			    if 	&amp;amp;v4._dd^=.    then &amp;amp;ovar.4=put(&amp;amp;v4._yyyy, 4.)||'-'||put(&amp;amp;v4._mm, z2.)||'-'||put(&amp;amp;v4._dd, z2.);
			else if &amp;amp;v4._mm^=.    then &amp;amp;ovar.4=put(&amp;amp;v4._yyyy, 4.)||'-'||put(&amp;amp;v4._mm, z2.)||'-01';
			else if &amp;amp;v4._yyyy^=.  then &amp;amp;ovar.4=put(&amp;amp;v4._yyyy, 4.)||'-01'||'-01';
			&amp;amp;fvar.4=input(&amp;amp;ovar.4,yymmdd10.);	format &amp;amp;fvar.4 yymmdd10.;

       array dt3 &amp;amp;fvar. &amp;amp;fvar.2 &amp;amp;fvar.3 &amp;amp;fvar.4;
			 do over dt3;
			  if dt3=. then dt3="01JAN1900"d;
			 end;
			 _lastf=max(of dt3[*]);
		%end;	
    format _lastf yymmdd10.; 
run;

proc sort;by subject decending _lastf;run;
proc sort data=&amp;amp;ds. out=&amp;amp;ds.f nodupkey;by subject;run;
run;

%if &amp;amp;ord&amp;lt;=-1 %then %do;
proc append base=af  Data=&amp;amp;ds.f;
%end;
%else %if &amp;amp;ord=1 %then %do;
proc append base=bf  Data=&amp;amp;ds.f;
%end;
%else %if &amp;amp;ord=2 %then %do;
proc append base=cf  Data=&amp;amp;ds.f;
%end;
 
%else %if &amp;amp;ord=3 %then %do;
proc append base=df  Data=&amp;amp;ds.f;
%end;
run;
%mend;
/*pd(ord,ds, nvar,ovar,fvar,v1,v2,v3,v4);*/	 
%pd(3,a,  lstdt, last, adat, a2dat,a3dat,a4dat)
%pd(2,b,  lstdt, last, b1dat,b2dat,b3dat)
%pd(1,c,  lstdt, last, c1dat,c2dat, , ) 
%pd(-1,d, lstdt, last, ddat , , , )	
%pd(-2,e, lstdt, last, edat , , , )	
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Jun 2022 18:06:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-this-macro-code-to-calculate-mulitple-dates-from/m-p/820294#M323759</guid>
      <dc:creator>purpleclothlady</dc:creator>
      <dc:date>2022-06-24T18:06:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to simplify this macro code to calculate mulitple dates from multiple dataset? thanks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-this-macro-code-to-calculate-mulitple-dates-from/m-p/820297#M323762</link>
      <description>&lt;P&gt;What does it DO?&amp;nbsp; Please explain in words the purpose of the code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to pass a list of variable names just use ONE parameter.&lt;/P&gt;
&lt;P&gt;If you want to pass in a dataset name pass in the whole dataset name.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro pd(ds,ovar,fvar,varlist);
%local count lastvar ;
%let varlist=%sysfunc(compbl(&amp;amp;varlist));
%let count=%sysfunc(countw(&amp;amp;varlist,%str( )));
%let lastvar=%scan(&amp;amp;varlist,-1,%str( ));
...
   set &amp;amp;ds(keep= subject %sysfunc(tranwrd(&amp;amp;varlist,%str( ),%str(: ))): );
....
%mend pd;
%pd
(ds=new.a
,ovar=lstdt
,fvar=last
,varlist=adat a2dat a3dat a4dat
)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Jun 2022 18:18:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-this-macro-code-to-calculate-mulitple-dates-from/m-p/820297#M323762</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-06-24T18:18:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to simplify this macro code to calculate mulitple dates from multiple dataset? thanks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-this-macro-code-to-calculate-mulitple-dates-from/m-p/820300#M323764</link>
      <description>Hi Tom:&lt;BR /&gt;Thank you for quick  help. please see below. thanks &lt;BR /&gt;&lt;BR /&gt;The purpose of the code is to  find the LATEST date from mulitple datasets by Subject.&lt;BR /&gt;First:  To create a date variable by  dat_yyyy, dat_mm, dat_dd&lt;BR /&gt;          from multiple datasets and each datasets has different number of date variables , such as &lt;BR /&gt;          Dataset a: has 4 dates , b has 3.....&lt;BR /&gt;&lt;BR /&gt;Second:  Then find out the latest date from Each data sets (eg. Data set a: a1dat, a2dat, a3dat, a4dat.) - a3dat is the last date&lt;BR /&gt;&lt;BR /&gt;Third: Get the latest date from all datasets by Subject (eg from Dataset a, b, c ,d), the last date is from Dataset c-c1dat&lt;BR /&gt;</description>
      <pubDate>Fri, 24 Jun 2022 18:40:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-this-macro-code-to-calculate-mulitple-dates-from/m-p/820300#M323764</guid>
      <dc:creator>purpleclothlady</dc:creator>
      <dc:date>2022-06-24T18:40:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to simplify this macro code to calculate mulitple dates from multiple dataset? thanks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-this-macro-code-to-calculate-mulitple-dates-from/m-p/820302#M323766</link>
      <description>&lt;P&gt;Where is the list of datasets?&lt;/P&gt;
&lt;P&gt;How are you determining which variables are dates?&amp;nbsp; It kind of looks like you have to tell it which variables have dates?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want the latest date per SUBJECT across all observations in ONE dataset and&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;You know the dataset name&lt;/LI&gt;
&lt;LI&gt;You know the variable names (or prefixes as it kind of looks like you are doing)&lt;/LI&gt;
&lt;LI&gt;And the data is sorted by SUBJECT&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;Then just do:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let ds=HAVE;
%let varlist=avar1-avar4 labdate visitdate;
data want;
  set &amp;amp;ds;
  by subject ;
  retain latest;
  if first.subject the latest=.;
  latest=max(of latest &amp;amp;varlist);
  if last.subject;
  keep subject latest;
  format latest date9.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Jun 2022 18:50:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-this-macro-code-to-calculate-mulitple-dates-from/m-p/820302#M323766</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-06-24T18:50:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to simplify this macro code to calculate mulitple dates from multiple dataset? thanks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-this-macro-code-to-calculate-mulitple-dates-from/m-p/820304#M323768</link>
      <description>&lt;P&gt;I have to say that whenever I see code like this involving Put and concatenation followed by an Input that someone doesn't know the right function. The &lt;FONT size="5"&gt;&lt;STRONG&gt;MDY&lt;/STRONG&gt; &lt;/FONT&gt;function takes a numeric month, day and year parameter to return a date.&lt;/P&gt;
&lt;P&gt;So I suspect that&lt;/P&gt;
&lt;PRE&gt;&amp;amp;fvar= mdy(coalesce(&amp;amp;v1._mm,1),coalesce(&amp;amp;v1._dd,1), &amp;amp;v1._yyyy); &lt;/PRE&gt;
&lt;P&gt;(The Coalesce function returns the first non-missing value in the list of parameters. So is an easy way to avoid "if not missing then do.)&lt;/P&gt;
&lt;P&gt;accomplishes the same thing as that ugly code.. Though the question might be why you are waiting to this step to create date values.&lt;/P&gt;
&lt;PRE&gt;			if 	&amp;amp;v1._dd      ^=. then &amp;amp;ovar.=put(&amp;amp;v1._yyyy, 4.)||'-'||put(&amp;amp;v1._mm, z2.)||'-'||put(&amp;amp;v1._dd, z2.);
			else if &amp;amp;v1._mm  ^=. then &amp;amp;ovar.=put(&amp;amp;v1._yyyy, 4.)||'-'||put(&amp;amp;v1._mm, z2.)||'-01';
			else if &amp;amp;v1._yyyy^=. then &amp;amp;ovar.=put(&amp;amp;v1._yyyy, 4.)||'-01'||'-01';
			&amp;amp;fvar.=input(&amp;amp;ovar.,yymmdd10.);	format &amp;amp;fvar. yymmdd10.;&lt;/PRE&gt;</description>
      <pubDate>Fri, 24 Jun 2022 19:02:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-this-macro-code-to-calculate-mulitple-dates-from/m-p/820304#M323768</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-06-24T19:02:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to simplify this macro code to calculate mulitple dates from multiple dataset? thanks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-this-macro-code-to-calculate-mulitple-dates-from/m-p/820317#M323779</link>
      <description>&lt;LI-CODE lang="sas"&gt;%macro pd(ord,datasetsname,newvar,last,v1,v2,v3,v4);
/*****
   adat, a2dat... they have to first use mdy function to make it a sasdate, currently these date variables are prefixed with adat, 
such as adat_yyyy, adat_mm, adat_dd , 
then find out the latest date from data sets a, b, c, d, e 
****/
%pd(3,a,  lstdt, last, adat, a2dat,a3dat,a4dat)
%pd(2,b,  lstdt, last, b1dat,b2dat,b3dat)
%pd(1,c,  lstdt, last, c1dat,c2dat, , ) 
%pd(-1,d, lstdt, last, ddat , , , )	
%pd(-2,e, lstdt, last, edat , , , )	&lt;/LI-CODE&gt;
&lt;P&gt;Hi Tom:&lt;/P&gt;
&lt;P&gt;Where is the list of datasets?&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000FF"&gt;&lt;STRONG&gt;Answe&lt;/STRONG&gt;r: the list of data sets are in the macro %pd- a, b , c ,d, e&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;How are you determining which variables are dates?&amp;nbsp; It kind of looks like you have to tell it which variables have dates?&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000FF"&gt;Answer: the macro %pd had parameters : v1, v2, v3,v4&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;Is this clear now? thanks again. &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;Purple&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Jun 2022 20:25:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-this-macro-code-to-calculate-mulitple-dates-from/m-p/820317#M323779</guid>
      <dc:creator>purpleclothlady</dc:creator>
      <dc:date>2022-06-24T20:25:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to simplify this macro code to calculate mulitple dates from multiple dataset? thanks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-this-macro-code-to-calculate-mulitple-dates-from/m-p/820320#M323780</link>
      <description>hi Ballardw: &lt;BR /&gt;Thanks for the suggestion. I will use the coalesce function. &lt;BR /&gt;Though the question might be why you are waiting to this step to create date values.-- I don't know what do you mean by this? &lt;BR /&gt;thanks&lt;BR /&gt;purple&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Fri, 24 Jun 2022 20:40:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-this-macro-code-to-calculate-mulitple-dates-from/m-p/820320#M323780</guid>
      <dc:creator>purpleclothlady</dc:creator>
      <dc:date>2022-06-24T20:40:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to simplify this macro code to calculate mulitple dates from multiple dataset? thanks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-this-macro-code-to-calculate-mulitple-dates-from/m-p/820324#M323781</link>
      <description>&lt;P&gt;You didn't really answer the question about how &lt;STRONG&gt;YOU know&lt;/STRONG&gt; which datasets and which variables.&amp;nbsp; Instead you just said what I already saw in the current code which is that you had to &lt;STRONG&gt;TELL&lt;/STRONG&gt; the macro which dataset to use and which variables to use.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is the list of datasets always A B C D E ?&lt;/P&gt;
&lt;P&gt;Is the list of variable names always some letter followed by DAT_ and either YYYY, MM or DD ?&lt;/P&gt;
&lt;P&gt;Why are there three variables per date? (is there any hope of make simplifications before the data gets to SAS)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why are you passing in that -2,-1,1,2,3 parameter when the macro can just COUNT how many variables you passed in?&lt;/P&gt;
&lt;P&gt;It seems that 1,2,3 is just a count of how many variables you passed.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What do the negative numbers mean?&amp;nbsp;What is difference between -1 and 1?&amp;nbsp; Between -2 and -1?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why no code to handle zero?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Explain what it means, not what the current code does.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What does that first data step do?&amp;nbsp; The one with so much macro code being used to generate it.&lt;/P&gt;
&lt;P&gt;How is it helping to get to the answer?&lt;/P&gt;
&lt;P&gt;Does it just convert the M,D,Y variables into actual dates?&amp;nbsp; Does it transpose the data?&amp;nbsp; Does it find the max?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What do the multiple PROC SORT steps do?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why are you creating 4 different datasets in the last block of macro code that is being driven by the ORD variable?&lt;/P&gt;
&lt;P&gt;Now the ORD variable seems to have a different meaning than it did in the first data step.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Rather than just meaning how many variables to process it seems to meaning what to name the output dataset.&lt;/P&gt;
&lt;P&gt;If the name of the output dataset changes why not just pass it in as a parameter like the input dataset?&lt;/P&gt;
&lt;P&gt;Or derive it based on the value of the ORD parameter?&amp;nbsp; You seem to be mapping -2 -&amp;gt; AF , -1 -&amp;gt;AF, 1-&amp;gt;BF ...&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let base = %scan(AF AF ZERO BF CF DF,&amp;amp;ord+3);
proc append base=&amp;amp;base  Data=&amp;amp;ds.f;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Jun 2022 20:46:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-this-macro-code-to-calculate-mulitple-dates-from/m-p/820324#M323781</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-06-24T20:46:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to simplify this macro code to calculate mulitple dates from multiple dataset? thanks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-this-macro-code-to-calculate-mulitple-dates-from/m-p/820415#M323817</link>
      <description>&lt;P&gt;Hi Tom:&lt;/P&gt;
&lt;P&gt;I answered all questions in the comment section in the code.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;please check if this is clear now.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;thanks&lt;/P&gt;
&lt;P&gt;Purple&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
options mprint mlogic;
%macro pd(ord,ds,ovar,fvar,v1,v2,v3,v4);
/*-----Is the list of datasets always A B C D E ?: 
***Answer: There are 30 datasets with different names. I just give an example. 

-----Is the list of variable names always some letter followed by DAT_ and either YYYY, MM or DD ?
***Answer: Yes , eg. birthdat_yyyy , birthdat_mm, birthdat_dd
-----Why are there three variables per date? (is there any hope of make simplifications before the data gets to SAS)
*** Answer: there are about 30 data sets, the below is the code I used to get the date from yyyy, dd, mm. &lt;BR /&gt;it is not pretty though.
---Why are you passing in that -2,-1,1,2,3 parameter when the macro can just COUNT how many variables you passed in?
It seems that 1,2,3 is just a count of how many variables you passed.  
***Right, we can get a better way.
---What do the negative numbers mean? What is difference between -1 and 1?  Between -2 and -1? 
Answer: I updated the code. this ord is used to count number of variables. 
---Why no code to handle zero?
Answer: what do you mean ? 
---What does that first data step do?  The one with so much macro code being used to generate it.
Answer: there are many variables in each dataset, I only need to Keep a few of them and the number of variables are different in the data sets.
---How is it helping to get to the answer? Does it just convert the M,D,Y variables into actual dates?  &lt;BR /&gt;   Does it transpose the data?  Does it find the max?  What do the multiple PROC SORT steps do?
Answer: First need Actual dates, the ultimate goal is to find from the 30 data sets the latest date by Subject.

---Why are you creating 4 different datasets in the last block of macro code that is being driven by the ORD variable?
Now the ORD variable seems to have a different meaning than it did in the first data step.  
Rather than just meaning how many variables to process it seems to meaning what to name the output dataset.
If the name of the output dataset changes why not just pass it in as a parameter like the input dataset?
Or derive it based on the value of the ORD parameter?  You seem to be mapping -2 -&amp;gt; AF , -1 -&amp;gt;AF, 1-&amp;gt;BF ...
Answer: Need to append all 30 datasets, please see Step 3, if I only use one proc append, &lt;BR /&gt;        there is a log WARNING: Variable LAST4 was not found on DATA file.

/*   Step 1). IMPUTE PARTIAL DATES FROM MANY DATA SETS WHICH HAD DIFFERENT NUMBER OF DATES; */
data &amp;amp;ds (keep=subject &amp;amp;fvar.: ds _lastf);
length ds $10.;
   set ae06june.&amp;amp;ds(keep= subject 
                   %if &amp;amp;ord=1       %then %do; &amp;amp;v1.:  %end;
                   %else %if &amp;amp;ord=2  %then %do; &amp;amp;v1.: &amp;amp;v2.: %end;
                   %else %if &amp;amp;ord=3  %then %do; &amp;amp;v1.: &amp;amp;v2.: &amp;amp;v3.: %end;
                   %else %if &amp;amp;ord=4  %then %do; &amp;amp;v1.: &amp;amp;v2.: &amp;amp;v3.: &amp;amp;v4.:%end;
                   );		

/*IF USE MDY , THEN LOG  HAS "NOTE: MISSING VALUES, HOW TO REMOVE THE LOG NOTE OF "MISSING VALUES...."*/			
			if 	&amp;amp;v1._dd      ^=. then &amp;amp;ovar.=put(&amp;amp;v1._yyyy, 4.)||'-'||put(&amp;amp;v1._mm, z2.)||'-'||put(&amp;amp;v1._dd, z2.);
			else if &amp;amp;v1._mm  ^=. then &amp;amp;ovar.=put(&amp;amp;v1._yyyy, 4.)||'-'||put(&amp;amp;v1._mm, z2.)||'-01';
			else if &amp;amp;v1._yyyy^=. then &amp;amp;ovar.=put(&amp;amp;v1._yyyy, 4.)||'-01'||'-01';
			&amp;amp;fvar.=input(&amp;amp;ovar.,yymmdd10.);	format &amp;amp;fvar. yymmdd10.;
			_lastf=&amp;amp;fvar.;
		
		  %if &amp;amp;ord&amp;gt;=2 %then %do;		 	 
			if 	&amp;amp;v2._dd^=.        then &amp;amp;ovar.2=put(&amp;amp;v2._yyyy, 4.)||'-'||put(&amp;amp;v2._mm, z2.)||'-'||put(&amp;amp;v2._dd, z2.);
			else if &amp;amp;v2._mm^=.    then &amp;amp;ovar.2=put(&amp;amp;v2._yyyy, 4.)||'-'||put(&amp;amp;v2._mm, z2.)||'-01';
			else if &amp;amp;v2._yyyy^=.  then &amp;amp;ovar.2=put(&amp;amp;v2._yyyy, 4.)||'-01'||'-01';
			&amp;amp;fvar.2=input(&amp;amp;ovar.2,yymmdd10.);	format &amp;amp;fvar.2 yymmdd10.;

			 array dt &amp;amp;fvar. &amp;amp;fvar.2;
			  do over dt;
			  if dt=. then dt="01JAN1900"d;
			 end;
			 _lastf=max(of dt[*]);
		%end;
		 %if &amp;amp;ord&amp;gt;=3 %then %do;
			    if 	&amp;amp;v3._dd^=.    then &amp;amp;ovar.3=put(&amp;amp;v3._yyyy, 4.)||'-'||put(&amp;amp;v3._mm, z2.)||'-'||put(&amp;amp;v3._dd, z2.);
			else if &amp;amp;v3._mm^=.    then &amp;amp;ovar.3=put(&amp;amp;v3._yyyy, 4.)||'-'||put(&amp;amp;v3._mm, z2.)||'-01';
			else if &amp;amp;v3._yyyy^=.  then &amp;amp;ovar.3=put(&amp;amp;v3._yyyy, 4.)||'-01'||'-01';
			&amp;amp;fvar.3=input(&amp;amp;ovar.3,yymmdd10.);	format &amp;amp;fvar.3 yymmdd10.;

			 array dt2 &amp;amp;fvar. &amp;amp;fvar.2 &amp;amp;fvar.3;
			  do over dt2;
			  if dt2=. then dt2="01JAN1900"d;
			 end;
			 _lastf=max(of dt2[*]);
		%end;
		%if &amp;amp;ord=4 %then %do;
			    if 	&amp;amp;v4._dd^=.    then &amp;amp;ovar.4=put(&amp;amp;v4._yyyy, 4.)||'-'||put(&amp;amp;v4._mm, z2.)||'-'||put(&amp;amp;v4._dd, z2.);
			else if &amp;amp;v4._mm^=.    then &amp;amp;ovar.4=put(&amp;amp;v4._yyyy, 4.)||'-'||put(&amp;amp;v4._mm, z2.)||'-01';
			else if &amp;amp;v4._yyyy^=.  then &amp;amp;ovar.4=put(&amp;amp;v4._yyyy, 4.)||'-01'||'-01';
			&amp;amp;fvar.4=input(&amp;amp;ovar.4,yymmdd10.);	format &amp;amp;fvar.4 yymmdd10.;

       array dt3 &amp;amp;fvar. &amp;amp;fvar.2 &amp;amp;fvar.3 &amp;amp;fvar.4;
			 do over dt3;
			  if dt3=. then dt3="01JAN1900"d;
			 end;
			 _lastf=max(of dt3[*]);
		%end;	
    format _lastf yymmdd10.; 
run;
/*   Step 2).GET THE LATEST DATE FOR EACH DATA SET;*/
proc sort;by subject decending _lastf;run;
proc sort data=&amp;amp;ds. out=&amp;amp;ds.f nodupkey;by subject;run;
run;

/*  Step 3). APPEND ALL DATA SETS; BUT THIS STEP HAS TO USE PROC APPEND */
data all;
set af bf cf df ef ;
run;

/* Step 4) FIND THE LASTEST DATE FROM ALL DATA SETS*/
proc sort data=all;by subject decending _lastf;run;
data all;
set all;
by subject;
if last.subject;
run;

%mend;

/*pd(ord,ds, nvar,ovar,fvar,v1,v2,v3,v4);*/	 
%pd(4,e, lstdt, last, bicdat, cicdat,picdat,ficdat)
%pd(3,d, lstdt,last,  hopstdat,hopendat,testdat)
%pd(2,c, lstdt, last, castdat, caendat, , ) 
%pd(1,b, lstdt, last, bmdat , , , )	
%pd(1,a, lstdt, last, cldat , , , )
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 26 Jun 2022 04:59:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-this-macro-code-to-calculate-mulitple-dates-from/m-p/820415#M323817</guid>
      <dc:creator>purpleclothlady</dc:creator>
      <dc:date>2022-06-26T04:59:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to simplify this macro code to calculate mulitple dates from multiple dataset? thanks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-this-macro-code-to-calculate-mulitple-dates-from/m-p/820428#M323823</link>
      <description>&lt;P&gt;&amp;nbsp;I think you keep avoiding the question &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;has asked, which is: "You didn't really answer the question about how &lt;STRONG&gt;YOU know&lt;/STRONG&gt; which datasets and which variables."&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;What you said is NOT an answer:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;/*-----Is the list of datasets always A B C D E ?: &lt;BR /&gt;***Answer: There are 30 datasets with different names. I just give an example.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The programmer has to write code that selects data set names, but its still not clear how you know what data set names the program will select each time it runs. Please explain this. (Is it all data sets in a folder? or data set whose name begins with DATA_ and has the date appended such as DATA_02MAR2022 so you want all data set this month? or other? Please EXPLAIN)&lt;/P&gt;</description>
      <pubDate>Sun, 26 Jun 2022 11:35:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-this-macro-code-to-calculate-mulitple-dates-from/m-p/820428#M323823</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-06-26T11:35:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to simplify this macro code to calculate mulitple dates from multiple dataset? thanks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-this-macro-code-to-calculate-mulitple-dates-from/m-p/820431#M323824</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;I got it&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":face_savoring_food:"&gt;😋&lt;/span&gt; why you guys didn't understand. Sorry for the confusion.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the code I used to get&lt;/P&gt;
&lt;P&gt;"I &lt;SPAN&gt;&amp;nbsp;think you keep avoiding the question&amp;nbsp;&lt;/SPAN&gt;&lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159" target="_blank"&gt;@Tom&lt;/A&gt;&lt;SPAN&gt;&amp;nbsp;has asked, which is: "You didn't really answer the question about how&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;YOU know&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;which datasets and which variables."&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data lastd;
set sashelp.vcolumn(keep =memname libname memtype name
where=(libname="WORK" and memtype="DATA" and ( name like ('%DAT%'))));
run;

select distinct memname
into :dslist separated by " "
from lastd;
quit;
%put &amp;amp;dslist;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Purple&lt;/P&gt;</description>
      <pubDate>Sun, 26 Jun 2022 12:23:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-this-macro-code-to-calculate-mulitple-dates-from/m-p/820431#M323824</guid>
      <dc:creator>purpleclothlady</dc:creator>
      <dc:date>2022-06-26T12:23:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to simplify this macro code to calculate mulitple dates from multiple dataset? thanks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-this-macro-code-to-calculate-mulitple-dates-from/m-p/820435#M323825</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/347922"&gt;@purpleclothlady&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;I got it&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":face_savoring_food:"&gt;😋&lt;/span&gt; why you guys didn't understand. Sorry for the confusion.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the code I used to get&lt;/P&gt;
&lt;P&gt;"I &lt;SPAN&gt;&amp;nbsp;think you keep avoiding the question&amp;nbsp;&lt;/SPAN&gt;&lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159" target="_blank" rel="noopener"&gt;@Tom&lt;/A&gt;&lt;SPAN&gt;&amp;nbsp;has asked, which is: "You didn't really answer the question about how&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;YOU know&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;which datasets and which variables."&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data lastd;
set sashelp.vcolumn(keep =memname libname memtype name
where=(libname="WORK" and memtype="DATA" and ( name like ('%DAT%'))));
run;

select distinct memname
into :dslist separated by " "
from lastd;
quit;
%put &amp;amp;dslist;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Purple&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You can do this much easier from dictionary.tables:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint,
select memname into :dslist separated by " "
from dictionary.tables
where libname = "WORK" and memtype = "DATA" and name like '%DAT%';
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 26 Jun 2022 13:33:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-this-macro-code-to-calculate-mulitple-dates-from/m-p/820435#M323825</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-06-26T13:33:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to simplify this macro code to calculate mulitple dates from multiple dataset? thanks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-this-macro-code-to-calculate-mulitple-dates-from/m-p/820440#M323829</link>
      <description>&lt;P&gt;So you want to determine the variables to use by looking for variables named *DAT_YYYY or *DAT_MM or *DAT_DD in any dataset in the WORK library?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So first get the list of variables.&amp;nbsp; Parse out the basename and that extension. So a variable named ADAT_YYYY would have a basename of A and an extension of YYYY.&lt;/P&gt;
&lt;P&gt;Then transpose to convert the list to one observation per dataset/basename combination.&lt;/P&gt;
&lt;P&gt;Then generate code to set each dateset (once for each date triplet) and rename the variables to all be called YYYY, MM and DD.&lt;/P&gt;
&lt;P&gt;Then use a data step to read ALL of the data form all of the observations and convert the year month date values into dates and find the maximum per subject.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
create variables as
select catx('.',libname,memname) as dsname
     , substrn(name,1,index(upcase(name),'DAT_')-1) as basename
     , substrn(name,index(upcase(name),'DAT_')+4) as extension
     , nliteral(upcase(name)) as name
from dictionary.columns
where libname = "WORK" 
  and memtype = "DATA"
  and ((upcase(trim(name)) like '%DAT^_YYYY' escape '^'
    or (upcase(trim(name)) like '%DAT^_MM' escape '^'
    or (upcase(trim(name)) like '%DAT^_DD' escape '^'
      )
order by 1,2,3
;
quit;

proc transpose data=variables out=members(drop=_name_);
  by dsname basename;
  id extension;
  var name;
run;

filename code temp;
data _null_;
  set members end=eof;
  where 0=cmiss(of yyyy mm dd);
  if _n_=1 then put 'set' @;
  put @5 dsname '(keep=subject ' yyyy mm dd 
         'rename=(' yyyy '=yyyy ' mm '=mm ' dd '=dd))'
  ;
  if eof then put ';' ;
run;

data want;
  %include code / source2;
  where 0=cmiss(of yyyy mm dd);
  by subject;
  retain maxdate ;
  if first.subject then maxdate=.;
  maxdate=max(maxdate,mdy(mm,dd,yyy));
  if last.subject;
  format maxdate date9.;
  keep subject maxdate;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If the data is not sorted by SUBJECT then you might want to create that last step as a VIEW and then use PROC SUMMARY to find the max.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data all / view=all;
  %include code / source2;
  where 0=cmiss(of yyyy mm dd);
  maxdate=max(maxdate,mdy(mm,dd,yyy));
  format maxdate date9.;
  keep subject maxdate;
run;
proc summary data=all nway ;
  class subject ;
  var maxdate ;
  output out=want max= ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 27 Jun 2022 13:55:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-this-macro-code-to-calculate-mulitple-dates-from/m-p/820440#M323829</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-06-27T13:55:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to simplify this macro code to calculate mulitple dates from multiple dataset? thanks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-this-macro-code-to-calculate-mulitple-dates-from/m-p/820441#M323830</link>
      <description>&lt;P&gt;Thanks.&lt;/P&gt;
&lt;P&gt;Now take another step back in the process.&amp;nbsp; Where did the datasets in the WORK library come from?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Were they copied from some other SAS libref? Why were they copied?&lt;/P&gt;
&lt;P&gt;Were they generated by some program?&amp;nbsp; Why did the program not generate DATE variable to begin with?&lt;/P&gt;
&lt;P&gt;Were they created from some non-SAS files?&amp;nbsp; What type of files?&amp;nbsp; If they were text files why not generate the dates when the files were read?&lt;/P&gt;</description>
      <pubDate>Sun, 26 Jun 2022 17:57:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-this-macro-code-to-calculate-mulitple-dates-from/m-p/820441#M323830</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-06-26T17:57:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to simplify this macro code to calculate mulitple dates from multiple dataset? thanks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-this-macro-code-to-calculate-mulitple-dates-from/m-p/820527#M323870</link>
      <description>Hi Kurt:&lt;BR /&gt;Dictionary.tables gave me the list of Data set name, &lt;BR /&gt;if I also need to get variable names containing "%DAT%" , &lt;BR /&gt;&lt;BR /&gt;proc sql noprint;&lt;BR /&gt;create table havecol as&lt;BR /&gt;select *&lt;BR /&gt;from dictionary.columns&lt;BR /&gt;where libname = "WORK" and memtype = "DATA" and name like '%DAT%';&lt;BR /&gt;quit;&lt;BR /&gt;Thanks&lt;BR /&gt;Purple</description>
      <pubDate>Mon, 27 Jun 2022 13:33:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-this-macro-code-to-calculate-mulitple-dates-from/m-p/820527#M323870</guid>
      <dc:creator>purpleclothlady</dc:creator>
      <dc:date>2022-06-27T13:33:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to simplify this macro code to calculate mulitple dates from multiple dataset? thanks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-this-macro-code-to-calculate-mulitple-dates-from/m-p/820530#M323872</link>
      <description>&lt;P&gt;Tom and Kurt: &lt;BR /&gt;That is exactly what I need. thanks all for make the puzzle resolved. super. !&lt;BR /&gt;I need to test the code and let you know.&lt;/P&gt;
&lt;P&gt;thanks again,&lt;/P&gt;
&lt;P&gt;Purple&lt;/P&gt;</description>
      <pubDate>Mon, 27 Jun 2022 13:52:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-this-macro-code-to-calculate-mulitple-dates-from/m-p/820530#M323872</guid>
      <dc:creator>purpleclothlady</dc:creator>
      <dc:date>2022-06-27T13:52:08Z</dc:date>
    </item>
    <item>
      <title>Re: How to simplify this macro code to calculate mulitple dates from multiple dataset? thanks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-this-macro-code-to-calculate-mulitple-dates-from/m-p/820964#M324078</link>
      <description>&lt;P&gt;Hi all:&lt;/P&gt;
&lt;P&gt;I tested the code, maybe it is much easier by using the dataset and the current code, it worked but need a lot of repeat codes. a macro will be more efficient?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;data temp.have;
length ds $10;
set temp.a(in=a) temp.b(in=b) temp.c(in=c);
/*Q1: GIVEN THAT WE KNOW DATASET NAME AND VARIABLE NAMES BY USING DICTIONAY.
      BUT THERE ARE ABOUT 40 DATASETS LOOK LIKE THIS, HOW TO READ IN THESE DATASETS AND CREATE DATE USING MDY FUNCTION 
      WITHOUT SHOWING LOG--NOTE MISSING VALUES:
      " MISSING VALUES WERE GENERATED AS A RESULT OF PERFORMING AN OPERATION ON MISSING VALUES.
      EACH PLACE IS GIVEN BY: (NUMBER OF TIMES) AT (LINE):(COLUMN).
      2 AT 1403:14    10 AT 1416:15 "
*/
		if  a then do;
		  _adate=mdy(adat_mm, adat_dd,adat_yyyy);
		  _edate=mdy(edat_mm, edat_dd,edat_yyyy); 
		 ds="DATA SET A";
		end;

		else if  b then do;
			 _brdate=mdy(brdat_mm, brdat_dd,brdat_yyyy);
      ds="DATA SET B";
     end;

    else if c then do;
			 _idate=mdy(idat_mm, idat_dd,idat_yyyy);
		   _bndate=mdy(bndat_mm, bndat_dd,bndat_yyyy); 
		   _tdate=mdy(tdat_mm,tdat_dd,tdat_yyyy);
		   _pdate=mdy(pdat_mm, pdat_dd,pdat_yyyy); 
		 ds="DATA SET C" ;
		end;
		 
		_maxdate=max(of _numeric_);
		format _: mmddyy10.;
		keep id ds _maxdate _a: _bn: _i: _t: _p: _br:;	
run;

/*proc sort data=temp.have;by id _maxdate;run;*/

proc summary data=temp.have nway ;
  class id ;
  var _maxdate ;
  output out=temp.want max=;
run;
&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 29 Jun 2022 16:19:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-this-macro-code-to-calculate-mulitple-dates-from/m-p/820964#M324078</guid>
      <dc:creator>purpleclothlady</dc:creator>
      <dc:date>2022-06-29T16:19:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to simplify this macro code to calculate mulitple dates from multiple dataset? thanks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-this-macro-code-to-calculate-mulitple-dates-from/m-p/820968#M324081</link>
      <description>&lt;P&gt;Define what you mean by efficient?&amp;nbsp; &amp;nbsp;For the code to RUN? Or for the programmer to generate?&amp;nbsp; If you use macro to generate same code you were already running there is no change in how it will actually run.&amp;nbsp; It might make it easier for the programmer.&amp;nbsp; Or it might just add complexity and make it harder for the programmer.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does this new code cover all of the possible m,d,y variables?&lt;/P&gt;
&lt;P&gt;For this particular application your code looks over complicated.&amp;nbsp; If some observations have missing values those missing values will not impact the maximum date calculated.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp.have;
  set temp.a temp.b temp.c indsname=indsname;
  ds = indsname ;
  _adate=mdy(adat_mm, adat_dd,adat_yyyy);
  _edate=mdy(edat_mm, edat_dd,edat_yyyy); 
  _brdate=mdy(brdat_mm, brdat_dd,brdat_yyyy);
  _idate=mdy(idat_mm, idat_dd,idat_yyyy);
  _bndate=mdy(bndat_mm, bndat_dd,bndat_yyyy); 
  _tdate=mdy(tdat_mm,tdat_dd,tdat_yyyy);
  _pdate=mdy(pdat_mm, pdat_dd,pdat_yyyy); 
  _maxdate=max(of _adate _edate _brdate _idate _bndate _tdate _pdate );
  format _maxdate _adate _edate _brdate _idate _bndate _tdate _pdate yymmdd10.;
  keep id ds _maxdate _adate _edate _brdate _idate _bndate _tdate _pdate;	
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could in theory make a macro that takes BR as input and generates&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;_brdate=mdy(brdat_mm, brdat_dd,brdat_yyyy);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;but why?&amp;nbsp; It would just make the code more confusing.&amp;nbsp; Are these names going to change in the future? Why would they change?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to eliminate the notes about generating the missing values you could just confirm that you actually have month day and year values.&amp;nbsp; So if the variables are numeric you could do:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if 0=nmiss(adat_mm, adat_dd,adat_yyyy) then _adate=mdy(adat_mm, adat_dd,adat_yyyy);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;It the notes are on the MAX() function call you could check there also.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if 0=nmiss(of  _adate _edate _brdate _idate _bndate _tdate _pdate ) then
   _maxdate=max(of _adate _edate _brdate _idate _bndate _tdate _pdate )
;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 29 Jun 2022 17:14:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-this-macro-code-to-calculate-mulitple-dates-from/m-p/820968#M324081</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-06-29T17:14:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to simplify this macro code to calculate mulitple dates from multiple dataset? thanks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-this-macro-code-to-calculate-mulitple-dates-from/m-p/821022#M324114</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;:&lt;/P&gt;
&lt;P&gt;Thank you all for your continuous support. I appreciate that and learnt a lot.&lt;span class="lia-unicode-emoji" title=":folded_hands:"&gt;🙏&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Here is the final code I worked and it gave me the correct result.&lt;/P&gt;
&lt;P&gt;One thing I need to improve: Since this is for many different name of date variables, I would also like to know how to create a macro to use mdy function. But with that being said, I did finish the project.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;purple&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;/*---STEP 1: FIND OUT HOW MANY DATA SETS AND VARIABLE NAME WITH CONTAINS '%DAT%'---*/
proc sql noprint;
create table havecol as
select *, cats(strip(libname),".", memname) as newmem
from dictionary.columns
where libname = "TEMP" and memtype = "DATA" and 
       ( (name like '%DAT_YYYY') or ( name like '%DAT_MM') or ( name like '%DAT_DD')  );
quit;

/*---STEP 2: PUT INTO MACRO VAR THE DATA SET NAME ---*/
proc sql noprint;
 select count(distinct newmem) 
        into :dsn  trimmed
 from havecol;

 select distinct newmem
        into  :ds1 - :ds&amp;amp;dsn 
 from havecol;
 quit;
%put &amp;amp;dsn  &amp;amp;ds1 &amp;amp;ds2 &amp;amp;ds3 ;


/*---STEP 3: APPEND ALL DATA SETS ---*/
options mprint mlogic;
%macro h2;
 data temp.have;
 length source $10;
  set 
    %do i=1 %to &amp;amp;dsn;
        &amp;amp;&amp;amp;ds&amp;amp;i  indsname=indsname
		 %end;
		;
		SOURCE=indsname;

	 /*--A. CREATE DATE VARIABLE BASED ON YYYY, MM DD--*/
   _adate=mdy(adat_mm, adat_dd,adat_yyyy);
  _edate=mdy(edat_mm, edat_dd,edat_yyyy); 
  _brdate=mdy(brdat_mm, brdat_dd,brdat_yyyy);
  _hospstndate=mdy(hospstdat_mm, hospstdat_dd,hospstdat_yyyy); 
  _dsstdate=mdy(dsstdat_mm,dsstdat_dd,dsstdat_yyyy);
  _awardate=mdy(awardat_mm, awardat_dd,awardat_yyyy);

	/*--B. FIND OUT MAX VALUE OF DATE PER ROW--*/
   	_maxdate=max(of _numeric_);
		format _: mmddyy10.;
		keep id source _maxdate _:;	
run;
%mend;
%h2;

proc summary data=have2 nway ;
  class id ;
  var _maxdate ;
  output out=temp.want(keep=id _maxdate) max=;
run;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jun 2022 20:37:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-this-macro-code-to-calculate-mulitple-dates-from/m-p/821022#M324114</guid>
      <dc:creator>purpleclothlady</dc:creator>
      <dc:date>2022-06-29T20:37:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to simplify this macro code to calculate mulitple dates from multiple dataset? thanks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-this-macro-code-to-calculate-mulitple-dates-from/m-p/821052#M324137</link>
      <description>&lt;P&gt;Seems like an over complication to generate multiple macro variables.&amp;nbsp; Do you have more than a thousand datasets?&amp;nbsp; You can fit 65K characters into a single macro variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select distinct newmem into :dsnames separated by ' '
 from havecol
;
quit;

data temp.have;
  length source $41;
  set &amp;amp;dsnames indsname=indsname;
  SOURCE=indsname;
...&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 30 Jun 2022 00:51:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-this-macro-code-to-calculate-mulitple-dates-from/m-p/821052#M324137</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-06-30T00:51:18Z</dc:date>
    </item>
  </channel>
</rss>

