<?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: fetch each subjects corresponding records from multiple datasets in a library in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/fetch-each-subjects-corresponding-records-from-multiple-datasets/m-p/930784#M366193</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data A;
infile datalines;
input Subjid $ Start_date ddmmyy10.;
format start_date e8601da.;
datalines;
Subj1 01/01/2001
Subj2 02/02/2002
Subj3 03/03/2003
Subj4 04/04/2004
;
run;

data B;
infile datalines;
input Subjid $ end_date ddmmyy10.;
format end_date e8601da.;
datalines;
Subj1 01/01/2021
Subj2 02/02/2022
Subj3 03/03/2023
Subj4 04/04/2024
;
run;

Data C;
infile datalines;
input Subjid $ lab_date ddmmyy10.;
format lab_date e8601da.;
datalines;
Subj1 01/01/1999
Subj2 02/02/2001
Subj3 03/03/2001
Subj4 04/04/2025
;
run;

data D;
infile datalines;
input Subjid $ Test_date ddmmyy10.;
format test_date e8601da.;
datalines;
Subj1 01/01/2011
Subj2 02/02/2011
Subj3 03/03/2025
Subj4 04/04/2014
;
run;

proc sql;
	create table a_b as 
	select a.*
		  ,b.end_date
	from a natural join b;
quit;

options mprint;
%macro checkForViolation(datasets=C#D,dates=lab_date#test_date);
	%local i;

	%do i=1 %to %sysfunc(countw(&amp;amp;datasets, #));
		proc sql;
			create table %scan(&amp;amp;datasets, &amp;amp;i, #)_violate as
				select l.* 
				from %scan(&amp;amp;datasets, &amp;amp;i, #) as l left join a_b as r
				on l.subjid=r.subjid
				where l.%scan(&amp;amp;dates, &amp;amp;i, #) &amp;lt; r.start_date or l.%scan(&amp;amp;dates, &amp;amp;i, #) &amp;gt; r.end_date;
		quit;
	%end;
%mend checkForViolation;


%checkForViolation;
	
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Is this what you mean&lt;/P&gt;</description>
    <pubDate>Tue, 04 Jun 2024 11:01:24 GMT</pubDate>
    <dc:creator>Mazi</dc:creator>
    <dc:date>2024-06-04T11:01:24Z</dc:date>
    <item>
      <title>fetch each subjects corresponding records from multiple datasets in a library</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fetch-each-subjects-corresponding-records-from-multiple-datasets/m-p/930498#M366100</link>
      <description>Hello, I would like to fetch each subjects corresponding records from multiple datasets in a library where date criteria fails.&lt;BR /&gt;&lt;BR /&gt;Example&lt;BR /&gt;Dataset A&lt;BR /&gt;Subjid. Start date&lt;BR /&gt;Subj1 01/01/2001&lt;BR /&gt;Subj2. 02/02/2002&lt;BR /&gt;Subj3. 03/03/2003&lt;BR /&gt;Subj4. 04/04/2004&lt;BR /&gt;&lt;BR /&gt;Dataset B&lt;BR /&gt;Subjid. End date&lt;BR /&gt;&lt;BR /&gt;Subj1 01/01/2021&lt;BR /&gt;Subj2. 02/02/2022&lt;BR /&gt;Subj3. 03/03/2023&lt;BR /&gt;Subj4. 04/04/2024&lt;BR /&gt;&lt;BR /&gt;Dataset C&lt;BR /&gt;Subjid Lab date&lt;BR /&gt;Subj1 01/01/1999&lt;BR /&gt;Subj2. 02/02/2001&lt;BR /&gt;Subj3. 03/03/2001&lt;BR /&gt;Subj4. 04/04/2025&lt;BR /&gt;&lt;BR /&gt;Dataset D&lt;BR /&gt;Subjid Test date. Edit date&lt;BR /&gt;Subj1 01/01/2011 01/01/2020&lt;BR /&gt;Subj2. 02/02/2011 02/02/2023&lt;BR /&gt;Subj3. 03/03/2025 03/03/2013&lt;BR /&gt;Subj4. 04/04/2014 04/04/2014&lt;BR /&gt;&lt;BR /&gt;From above example report should have below that violated date rule -&lt;BR /&gt;&lt;BR /&gt;Dataset C&lt;BR /&gt;&lt;BR /&gt;Subjid Lab date&lt;BR /&gt;Subj1 01/01/1999&lt;BR /&gt;Subj4. 04/04/2025&lt;BR /&gt;&lt;BR /&gt;Dataset D&lt;BR /&gt;Subjid Test date. Edit date&lt;BR /&gt;Subj2. 02/02/2023&lt;BR /&gt;Subj3. 03/03/2025&lt;BR /&gt;&lt;BR /&gt;I would like to fetch subjects from dataset C and D where subjects with date(s) before dataset A start date and after dataset B end date. I have about 52 datasets with dates to be checked , most has different date variable&lt;BR /&gt;&lt;BR /&gt;Kindly help!</description>
      <pubDate>Sat, 01 Jun 2024 03:07:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fetch-each-subjects-corresponding-records-from-multiple-datasets/m-p/930498#M366100</guid>
      <dc:creator>SAS-PD</dc:creator>
      <dc:date>2024-06-01T03:07:45Z</dc:date>
    </item>
    <item>
      <title>Re: fetch each subjects corresponding records from multiple datasets in a library</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fetch-each-subjects-corresponding-records-from-multiple-datasets/m-p/930784#M366193</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data A;
infile datalines;
input Subjid $ Start_date ddmmyy10.;
format start_date e8601da.;
datalines;
Subj1 01/01/2001
Subj2 02/02/2002
Subj3 03/03/2003
Subj4 04/04/2004
;
run;

data B;
infile datalines;
input Subjid $ end_date ddmmyy10.;
format end_date e8601da.;
datalines;
Subj1 01/01/2021
Subj2 02/02/2022
Subj3 03/03/2023
Subj4 04/04/2024
;
run;

Data C;
infile datalines;
input Subjid $ lab_date ddmmyy10.;
format lab_date e8601da.;
datalines;
Subj1 01/01/1999
Subj2 02/02/2001
Subj3 03/03/2001
Subj4 04/04/2025
;
run;

data D;
infile datalines;
input Subjid $ Test_date ddmmyy10.;
format test_date e8601da.;
datalines;
Subj1 01/01/2011
Subj2 02/02/2011
Subj3 03/03/2025
Subj4 04/04/2014
;
run;

proc sql;
	create table a_b as 
	select a.*
		  ,b.end_date
	from a natural join b;
quit;

options mprint;
%macro checkForViolation(datasets=C#D,dates=lab_date#test_date);
	%local i;

	%do i=1 %to %sysfunc(countw(&amp;amp;datasets, #));
		proc sql;
			create table %scan(&amp;amp;datasets, &amp;amp;i, #)_violate as
				select l.* 
				from %scan(&amp;amp;datasets, &amp;amp;i, #) as l left join a_b as r
				on l.subjid=r.subjid
				where l.%scan(&amp;amp;dates, &amp;amp;i, #) &amp;lt; r.start_date or l.%scan(&amp;amp;dates, &amp;amp;i, #) &amp;gt; r.end_date;
		quit;
	%end;
%mend checkForViolation;


%checkForViolation;
	
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Is this what you mean&lt;/P&gt;</description>
      <pubDate>Tue, 04 Jun 2024 11:01:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fetch-each-subjects-corresponding-records-from-multiple-datasets/m-p/930784#M366193</guid>
      <dc:creator>Mazi</dc:creator>
      <dc:date>2024-06-04T11:01:24Z</dc:date>
    </item>
    <item>
      <title>Re: fetch each subjects corresponding records from multiple datasets in a library</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fetch-each-subjects-corresponding-records-from-multiple-datasets/m-p/930794#M366202</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You wrote: "&lt;SPAN&gt;&amp;nbsp;I have about 52 datasets with dates to be checked , most has different date variable&lt;/SPAN&gt;"&lt;/P&gt;
&lt;P&gt;Do you have a list of those files? or maybe they are located in a single directory/libname?&lt;/P&gt;
&lt;P&gt;Do you have any rule for recognising that date variables? e.g., the end with "...DT" or "...date" ? Or maybe you have a list of those variables?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Jun 2024 12:12:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fetch-each-subjects-corresponding-records-from-multiple-datasets/m-p/930794#M366202</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2024-06-04T12:12:47Z</dc:date>
    </item>
    <item>
      <title>Re: fetch each subjects corresponding records from multiple datasets in a library</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fetch-each-subjects-corresponding-records-from-multiple-datasets/m-p/930795#M366203</link>
      <description>&lt;P&gt;Are you sure you want it to be:&lt;/P&gt;
&lt;P&gt;"&lt;SPAN&gt;subjects with date(s) before dataset A start date &lt;STRONG&gt;and&lt;/STRONG&gt; after dataset B end date.&lt;/SPAN&gt;"&lt;/P&gt;
&lt;P&gt;I think it should be:&lt;/P&gt;
&lt;P&gt;"&lt;SPAN&gt;subjects with date(s) before dataset A start date &lt;STRONG&gt;OR&lt;/STRONG&gt; after dataset B end date.&lt;/SPAN&gt;"&lt;/P&gt;
&lt;P&gt;?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Tue, 04 Jun 2024 12:14:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fetch-each-subjects-corresponding-records-from-multiple-datasets/m-p/930795#M366203</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2024-06-04T12:14:40Z</dc:date>
    </item>
    <item>
      <title>Re: fetch each subjects corresponding records from multiple datasets in a library</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fetch-each-subjects-corresponding-records-from-multiple-datasets/m-p/931153#M366339</link>
      <description>&lt;P&gt;All datasets are in the same&amp;nbsp;&lt;SPAN&gt;directory/libname, each dataset have different date variable that I need to check against start date and end date.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I am trying to find out all the subjects /records where the date entered is before start date OR after end date. Start date and end date comes from 2 different datasets and those 2 dates are checked with all dates entered in other 52 datasets in the same&amp;nbsp;directory/libname.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Jun 2024 15:13:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fetch-each-subjects-corresponding-records-from-multiple-datasets/m-p/931153#M366339</guid>
      <dc:creator>SAS-PD</dc:creator>
      <dc:date>2024-06-06T15:13:55Z</dc:date>
    </item>
    <item>
      <title>Re: fetch each subjects corresponding records from multiple datasets in a library</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fetch-each-subjects-corresponding-records-from-multiple-datasets/m-p/931208#M366362</link>
      <description>&lt;P&gt;Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/466333"&gt;@SAS-PD&lt;/a&gt;&amp;nbsp;, Did you try the solution I gave you above? Also, if all these datasets are in the same library, does that mean you have 54 datasets? 2 for the start and end dates and 52 to check against. Lastly as mentioned by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35763"&gt;@yabwon&lt;/a&gt;&amp;nbsp;do the date variables have common prefix or suffixes we can use to write logic to easily select them dynamically?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jun 2024 06:14:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fetch-each-subjects-corresponding-records-from-multiple-datasets/m-p/931208#M366362</guid>
      <dc:creator>Mazi</dc:creator>
      <dc:date>2024-06-07T06:14:27Z</dc:date>
    </item>
    <item>
      <title>Re: fetch each subjects corresponding records from multiple datasets in a library</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fetch-each-subjects-corresponding-records-from-multiple-datasets/m-p/931212#M366364</link>
      <description>&lt;P&gt;Is subjid unique in all datasets?&lt;/P&gt;
&lt;P&gt;What are the names for the date variables in all the other datasets?&lt;/P&gt;
&lt;P&gt;Is subjid character or numeric?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Not that the last question would be unnecessary if you had posted your example data in the proper manner - working DATA steps with DATALINES.&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jun 2024 07:34:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fetch-each-subjects-corresponding-records-from-multiple-datasets/m-p/931212#M366364</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-06-07T07:34:26Z</dc:date>
    </item>
    <item>
      <title>Re: fetch each subjects corresponding records from multiple datasets in a library</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fetch-each-subjects-corresponding-records-from-multiple-datasets/m-p/931261#M366384</link>
      <description>&lt;P&gt;Hi Mazi, thank you for your response! above code did work and fetched records that did not fall between start and end date. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; There are total of 52 datasets in the library including 2 with start and end date. There is no common prefix or suffix for a date variable, I need to check all the dates that are present in each dataset.&amp;nbsp; For data cleaning I am trying to check if there are any records that has date not between start and end date in the same library.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jun 2024 15:34:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fetch-each-subjects-corresponding-records-from-multiple-datasets/m-p/931261#M366384</guid>
      <dc:creator>SAS-PD</dc:creator>
      <dc:date>2024-06-07T15:34:56Z</dc:date>
    </item>
    <item>
      <title>Re: fetch each subjects corresponding records from multiple datasets in a library</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fetch-each-subjects-corresponding-records-from-multiple-datasets/m-p/931266#M366386</link>
      <description>&lt;P&gt;Hi Kurt, thank you for your response!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is subjid unique in all datasets? --&amp;gt;yes&lt;/P&gt;&lt;P&gt;What are the names for the date variables in all the other datasets? --&amp;gt;does not have any common prefix or suffix&lt;/P&gt;&lt;P&gt;Is subjid character or numeric? --&amp;gt; character&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sorry about not posting&amp;nbsp;&lt;SPAN&gt;working DATA steps with DATALINES...this is my first post and will keep this in mind moving forward.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jun 2024 15:39:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fetch-each-subjects-corresponding-records-from-multiple-datasets/m-p/931266#M366386</guid>
      <dc:creator>SAS-PD</dc:creator>
      <dc:date>2024-06-07T15:39:37Z</dc:date>
    </item>
    <item>
      <title>Re: fetch each subjects corresponding records from multiple datasets in a library</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fetch-each-subjects-corresponding-records-from-multiple-datasets/m-p/931286#M366391</link>
      <description>&lt;P&gt;So you can first merge all datasets by subjid. Then, retrieve the names of all numeric variables in this new dataset which are not your start and end dates (DICTIONARY.COLUMNS) into a macro variable. Use this macro variable to create an array, so you can loop through it and flag unwanted values.&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jun 2024 16:01:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fetch-each-subjects-corresponding-records-from-multiple-datasets/m-p/931286#M366391</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-06-07T16:01:40Z</dc:date>
    </item>
    <item>
      <title>Re: fetch each subjects corresponding records from multiple datasets in a library</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fetch-each-subjects-corresponding-records-from-multiple-datasets/m-p/931354#M366412</link>
      <description>&lt;P&gt;52 files?&amp;nbsp; Really?&amp;nbsp; &amp;nbsp;With (up to) 52 different date variable names?&amp;nbsp; &amp;nbsp;You should probably merge all the datasets by subjid, which would make a program data vector with START_DATE, END_DATE and 52 other date varibles.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You don't need to run a macro 52 times, but you can benefit from making 2 macrovars, each a list of 52 names, as I do with only 2 names below.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data A;
infile datalines;
input Subjid $ Start_date ddmmyy10.;
format start_date e8601da.;
datalines;
Subj1 01/01/2001
Subj2 02/02/2002
Subj3 03/03/2003
Subj4 04/04/2004
;
run;

data B;
infile datalines;
input Subjid $ end_date ddmmyy10.;
format end_date e8601da.;
datalines;
Subj1 01/01/2021
Subj2 02/02/2022
Subj3 03/03/2023
Subj4 04/04/2024
;
run;

Data C;
infile datalines;
input Subjid $ lab_date ddmmyy10.;
format lab_date e8601da.;
datalines;
Subj1 01/01/1999
Subj2 02/02/2001
Subj3 03/03/2001
Subj4 04/04/2025
;
run;

data D;
infile datalines;
input Subjid $ Test_date ddmmyy10.;
format test_date e8601da.;
datalines;
Subj1 01/01/2011
Subj2 02/02/2011
Subj3 03/03/2025
Subj4 04/04/2014
;
run;


%let dslist = c             d         ;
%let varlst = lab_date      test_date ;

data _null_;
  merge a b &amp;amp;dslist  end=end_of_merge;
  by subjid;

  array dates {*}  &amp;amp;varlst;

  if _n_=1 then do;
    declare hash h ;   
    declare hash hoh ();
      hoh.definekey('i');
      hoh.definedata('h');
      hoh.definedone();
    do i=1 to dim(dates);
      h=_new_ hash(ordered:'a');
      h.definekey('subjid');
      h.definedata('subjid','start_date','end_date',vname(dates{i}));
      h.definedone();
      hoh.add();
    end;
  end;
  do i=1 to dim(dates);
    if dates{i}^=.  and (dates{i}&amp;lt;start_date or dates{i}&amp;gt;end_date) then do;
      hoh.find();
      h.add();
    end;
  end;
  if end_of_merge then do i=1 to dim(dates);
    hoh.find();
    if h.num_items&amp;gt;0 then h.output(dataset:cats('exclude_',scan("&amp;amp;dslist",i)));
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To expand from 2 datasets (C and D) to 52, just expand the two macrovars DSLIST and VARLST.&amp;nbsp; None of the remaining code has to be changed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BTW, instead of 52 datasets, I think you would be far better off with 1 dataset, containing 52 dummy variables indicating whether the corresponding data variable is out of range.&amp;nbsp; Then you could just do filters on those dummy variables (or any combination of those dummy variables)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jun 2024 22:47:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fetch-each-subjects-corresponding-records-from-multiple-datasets/m-p/931354#M366412</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-06-07T22:47:09Z</dc:date>
    </item>
    <item>
      <title>Re: fetch each subjects corresponding records from multiple datasets in a library</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fetch-each-subjects-corresponding-records-from-multiple-datasets/m-p/931358#M366416</link>
      <description>&lt;P&gt;First question is do you really have non-standard variable names like that? With spaces in the name?&amp;nbsp; That will make the coding harder.&amp;nbsp; You will have to use name literals to reference the variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use the FMTINFO() function to find all of the variables that have a DATE type format attached to them (DATE, YYMMDD, etc.).&amp;nbsp; So if all of the datasets are in the library pointed to by the libref MYLIB the code would look like this.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc contents data=MYLIB._all_ noprint out=contents; run;
data datevars;
  set contents;
  where 'date'=fmtinfo(format,'cat');
  keep libname memname name;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now you can use this list to generate the code you want.&amp;nbsp; Do you want to process each dataset separately?&amp;nbsp; Let's assume you already have made the dataset with the three key variables (let's call them SUBJECT, START and END).&amp;nbsp; Let's call this dataset SUBJECTS.&amp;nbsp; Now for each dataset that has date variables you just want to do something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data bad_ds1 ;
  merge subjects(in=in1) mylib.ds1(in=in2);
  by subject;
  if in2;
  array dates datevar1 datevar2 ;
  anybad=0;
  do i=1 to dim(dates) until (anybad);
    anybad = (.Z &amp;lt; dates[i] &amp;lt; START) or (dates[i] &amp;gt; END);
  end;
  if anybad;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Which will make a subset of the data with just the observations where at least one of the DATE variables is populated but not in the range from START to END (where START and END are for that particular subject).&amp;nbsp; Note this assumes that all of the datasets are already sorted by the SUBJECT variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So the only thing that changes in that as you work you way through the list of data variables is dataset names and the list of variables in the ARRAY statement.&amp;nbsp; So you could easily generate it from the DATEVARS dataset.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename code temp;
data _null_;
  file code;
  set datevars;
  by memname ;
  if first.memname then put 
  'data bad_' memname ';'
/ '  merge subjects(in=in1) mylib.' memname '(in=in2);'
/ '  by subject;'
/ '  if in2;'
/ '  array dates ' @
  ;
  put name @;
  if last.memname then put 
  ';'
/ '  anybad=0;'
/ '  do i=1 to dim(dates) until (anybad);'
/ '    anybad = (.Z &amp;lt; dates[i] &amp;lt; START) or (dates[i] &amp;gt; END);'
/ '  end;'
/ '  if anybad;'
/ 'run;'
  ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 08 Jun 2024 00:28:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fetch-each-subjects-corresponding-records-from-multiple-datasets/m-p/931358#M366416</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-06-08T00:28:15Z</dc:date>
    </item>
    <item>
      <title>Re: fetch each subjects corresponding records from multiple datasets in a library</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fetch-each-subjects-corresponding-records-from-multiple-datasets/m-p/931520#M366456</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc datasets lib=work kill mt=data nolist nodetails nowarn;
quit;

data A;
infile datalines;
input Subjid $ Start_date ddmmyy10.;
format start_date e8601da.;
datalines;
Subj1 01/01/2001
Subj2 02/02/2002
Subj3 03/03/2003
Subj4 04/04/2004
;
run;

data B;
infile datalines;
input Subjid $ end_date ddmmyy10.;
format end_date e8601da.;
datalines;
Subj1 01/01/2021
Subj2 02/02/2022
Subj3 03/03/2023
Subj4 04/04/2024
;
run;

Data C;
infile datalines;
input Subjid $ lab_date ddmmyy10.;
format lab_date e8601da.;
datalines;
Subj1 01/01/1999
Subj2 02/02/2001
Subj3 03/03/2001
Subj4 04/04/2025
;
run;

data D;
infile datalines ;
input Subjid $ Test_date ddmmyy10.;
format test_date edit_date e8601da.;
edit_date = test_date;
datalines;
Subj1 01-01-2011
Subj2 01-02-2011
Subj3 03-03-2025
Subj4 04-04-2014
;
run;

data config;
	length data varname $32;
	infile datalines dlm=',' missover;
	input data $ varname $;
/*Add all datasets and each variable to check against in the config dataset*/
	datalines;
C,lab_date
D,test_date
D,edit_date
;
run;

options mprint symbolgen;
%macro check_for_violation;
	%local i obs operator j;
	proc sql;
		create table all_data as
			select memname from dictionary.tables 
		where libname='WORK' and memname not in ("A" "B" "CONFIG" "ALL_DATA");
		%let obs = &amp;amp;sqlObs;
		%do i=1 %to &amp;amp;sqlObs;
			%local ds&amp;amp;i;
		%end;
		select memname into: ds1- from all_data;
	quit;

	data _null_;
		set config;
		by data notsorted;
		if first.data then do;
			size=0;
			varcnt=0;
		end;
		size+1;
		varcnt+1;
		call symputx(catx('_', data, varcnt), varname,'l');
		if last.data then call symputx(catx('_', data, 'size'), varcnt,'l');
	run;
	
	%let operator=;
	proc sql;
		%do i=1 %to &amp;amp;obs;
			create table &amp;amp;&amp;amp;ds&amp;amp;i.._violate as
				select l.* 
				from &amp;amp;&amp;amp;ds&amp;amp;i as l left join 
					 a      as r 
				on l.subjid=r.subjid left join b as m
				on r.subjid=m.subjid
				where 
				%do j=1 %to %unquote(%nrstr(&amp;amp;)&amp;amp;&amp;amp;ds&amp;amp;i.._size);
					&amp;amp;operator
					(. &amp;lt; l.%unquote(%nrstr(&amp;amp;)&amp;amp;&amp;amp;ds&amp;amp;i.._&amp;amp;j) &amp;lt; r.start_date) or (l.%unquote(%nrstr(&amp;amp;)&amp;amp;&amp;amp;ds&amp;amp;i.._&amp;amp;j) &amp;gt; m.end_date &amp;gt; .)
					%let operator=or;
				%end;
				;
			%let operator=;	
		%end;
	quit;
%mend check_for_violation;


%check_for_violation;
	&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/466333"&gt;@SAS-PD&lt;/a&gt;&amp;nbsp;, I updated the code to use a config dataset to make it easier to specify datasets and their variables. All you would need to do is add each of your datasets to the config and the variables related to that dataset that you want checked. Since your variables do not follow any convention as you said earlier, this is the best I could come up with. I hope it helps.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Jun 2024 09:20:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fetch-each-subjects-corresponding-records-from-multiple-datasets/m-p/931520#M366456</guid>
      <dc:creator>Mazi</dc:creator>
      <dc:date>2024-06-10T09:20:39Z</dc:date>
    </item>
    <item>
      <title>Re: fetch each subjects corresponding records from multiple datasets in a library</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fetch-each-subjects-corresponding-records-from-multiple-datasets/m-p/931600#M366478</link>
      <description>&lt;P&gt;Thank you for your response!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The actual library has more than 52 datasets and about 115 dates variables to be checked against start and end dates. I agree with you to create 1 dataset with all 115+ dates variables from 52+ datasets (all dates variable in a librabry), subjid being the common variable among all datasets in the library. I was able to create a work dataset&amp;nbsp;that has all datasets with only date variables in library using&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;proc contents data = d_test._all_ out=content; run;&lt;/P&gt;&lt;P&gt;data content1;&lt;BR /&gt;set content;&lt;BR /&gt;where format = 'MMDDYY' ;&lt;BR /&gt;keep libname memname name n;&lt;BR /&gt;n+1;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;not sure how to proceed from here...kindly help!&lt;/P&gt;</description>
      <pubDate>Mon, 10 Jun 2024 19:04:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fetch-each-subjects-corresponding-records-from-multiple-datasets/m-p/931600#M366478</guid>
      <dc:creator>SAS-PD</dc:creator>
      <dc:date>2024-06-10T19:04:52Z</dc:date>
    </item>
    <item>
      <title>Re: fetch each subjects corresponding records from multiple datasets in a library</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fetch-each-subjects-corresponding-records-from-multiple-datasets/m-p/931604#M366482</link>
      <description>&lt;P&gt;Hello, thank you for your response!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I apologize as I am new to SAS... How do I sort all datasets in a library to subjid?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Jun 2024 19:16:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fetch-each-subjects-corresponding-records-from-multiple-datasets/m-p/931604#M366482</guid>
      <dc:creator>SAS-PD</dc:creator>
      <dc:date>2024-06-10T19:16:17Z</dc:date>
    </item>
    <item>
      <title>Re: fetch each subjects corresponding records from multiple datasets in a library</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fetch-each-subjects-corresponding-records-from-multiple-datasets/m-p/931608#M366484</link>
      <description>&lt;P&gt;I created a work dataset with all datasets with all date variables in library using Proc Content not sure how to proceed from here...kindly help!&lt;/P&gt;&lt;P&gt;data All_Dates;&lt;BR /&gt;infile datalines;&lt;BR /&gt;input libname $ memname $ name $ n;&lt;BR /&gt;datalines;&lt;BR /&gt;D_TEST C lab_date 1&lt;BR /&gt;D_TEST D Test_date 2&lt;BR /&gt;D_TEST E n_AEENDAT 3&lt;BR /&gt;D_TEST E n_AESITEAW 4&lt;BR /&gt;D_TEST F n_AESTDAT 5&lt;BR /&gt;D_TEST G VISIT_DATE 6&lt;BR /&gt;D_TEST G n_FTDAT 7&lt;BR /&gt;D_TEST G VISIT_DATE 8&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to check all dates to see if they are between D_TEST.A.STart_date and D_TEST.B.End_date&lt;/P&gt;</description>
      <pubDate>Mon, 10 Jun 2024 19:28:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fetch-each-subjects-corresponding-records-from-multiple-datasets/m-p/931608#M366484</guid>
      <dc:creator>SAS-PD</dc:creator>
      <dc:date>2024-06-10T19:28:52Z</dc:date>
    </item>
    <item>
      <title>Re: fetch each subjects corresponding records from multiple datasets in a library</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fetch-each-subjects-corresponding-records-from-multiple-datasets/m-p/931614#M366487</link>
      <description>&lt;P&gt;Is there a way to use dataset name and date variable from&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;proc contents data = d_test._all_ out=content; run;&lt;/P&gt;&lt;P&gt;data content1;&lt;BR /&gt;set content;&lt;BR /&gt;where format = 'MMDDYY' and name ^= 'EDIT_DATE';&lt;BR /&gt;keep libname memname name label n;&lt;BR /&gt;n+1;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Jun 2024 19:35:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fetch-each-subjects-corresponding-records-from-multiple-datasets/m-p/931614#M366487</guid>
      <dc:creator>SAS-PD</dc:creator>
      <dc:date>2024-06-10T19:35:28Z</dc:date>
    </item>
    <item>
      <title>Re: fetch each subjects corresponding records from multiple datasets in a library</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fetch-each-subjects-corresponding-records-from-multiple-datasets/m-p/931662#M366505</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/466333"&gt;@SAS-PD&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello, thank you for your response!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I apologize as I am new to SAS... How do I sort all datasets in a library to subjid?&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Given the dataset you already have, like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
set all_dates;
call execute("
  proc sort data="
!! catx(".",libname,memname) !!
  ";
  by subjid;
  run;
");
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 11 Jun 2024 07:57:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fetch-each-subjects-corresponding-records-from-multiple-datasets/m-p/931662#M366505</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-06-11T07:57:25Z</dc:date>
    </item>
    <item>
      <title>Re: fetch each subjects corresponding records from multiple datasets in a library</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fetch-each-subjects-corresponding-records-from-multiple-datasets/m-p/931663#M366506</link>
      <description>Yes, We absolutely can. instead of manually adding updating the config dataset, you can use this instead. Let me know if you still need it.</description>
      <pubDate>Tue, 11 Jun 2024 08:11:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fetch-each-subjects-corresponding-records-from-multiple-datasets/m-p/931663#M366506</guid>
      <dc:creator>Mazi</dc:creator>
      <dc:date>2024-06-11T08:11:19Z</dc:date>
    </item>
    <item>
      <title>Re: fetch each subjects corresponding records from multiple datasets in a library</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fetch-each-subjects-corresponding-records-from-multiple-datasets/m-p/931667#M366509</link>
      <description>&lt;P&gt;Another method to create code from data, by using a temporary file:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename incfile temp;

data _null_;
length line $200;
file incfile;
set all_datasets;
by libname memname;
if _n_ = 1
then do;
  line = "data " !! catx(".",libname,"all_combined") !! "; merge a b";
  put line;
end;
if first.memname
then do;
  line = catx(".",libname,memname) !! "(keep=subjid ";
  put line;
end;
put name;
if last.memname then put ")";
if done then put "; by subjid; run;";
run;

data _null_;
infile incfile truncover;
input line $200.;
put line;
run;
/* this step is for control; if the code in the log is OK, submit the following include */

%include incfile;

filename incfile clear;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;With the combined dataset, you can define an array over the date variables and run a check in a loop.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jun 2024 09:24:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fetch-each-subjects-corresponding-records-from-multiple-datasets/m-p/931667#M366509</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-06-11T09:24:13Z</dc:date>
    </item>
  </channel>
</rss>

