<?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: Assing Value based on Dates Range in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Assing-Value-based-on-Dates-Range/m-p/975453#M378123</link>
    <description>&lt;P&gt;Hi Tom, thanks a lot for reply! I think this is very brilliant, I hope I can write code like this after a lot of practice. In fact, the key word &lt;EM&gt;set&lt;/EM&gt;, &lt;EM&gt;retain&lt;/EM&gt;, and &lt;EM&gt;if.first/last&lt;/EM&gt; (and some other techniques that do not produce a visible output or a product that directly show how they work) are the techniques that I use least because I have not fully understand the logic (how they work) of them. It is good learning experience for me which let me know how to improve my code and thinking.&lt;/P&gt;</description>
    <pubDate>Mon, 22 Sep 2025 05:32:09 GMT</pubDate>
    <dc:creator>dxiao2017</dc:creator>
    <dc:date>2025-09-22T05:32:09Z</dc:date>
    <item>
      <title>Assing Value based on Dates Range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assing-Value-based-on-Dates-Range/m-p/975398#M378098</link>
      <description>&lt;P&gt;Hi Team,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have two datasets&amp;nbsp; one with EXDOSE start and end dates and another one is with ADT(Date)&lt;/P&gt;&lt;P&gt;How to assign the EXDOSE in second datset where ADT is between STDT and ENDT , if ADT is not fall between the STDT and ENDT then assign the EXDOSE which is before ADT like in below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data exdose;&lt;BR /&gt;length USUBJID $7;&lt;BR /&gt;format stdt endt date9.;&lt;BR /&gt;input USUBJID $ EXDOSE stdt :date9. endt :date9.;&lt;BR /&gt;datalines;&lt;BR /&gt;1361007 14 16MAY2025 18MAY2025&lt;BR /&gt;1361007 14 20MAY2025 30MAY2025&lt;BR /&gt;1361007 18 31MAY2025 14JUN2025&lt;BR /&gt;1361007 18 16JUN2025 18JUN2025&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data second_ds;&lt;BR /&gt;length USUBJID $7;&lt;BR /&gt;format adt date9.;&lt;BR /&gt;input USUBJID $&amp;nbsp; ADT :date9.;&lt;BR /&gt;datalines;&lt;BR /&gt;1361007 17MAY2025&amp;nbsp;&lt;BR /&gt;1361007 19MAY2025&lt;BR /&gt;1361007 30MAY2025&lt;BR /&gt;1361007 15JUN2025&amp;nbsp;&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Expect dataset :&lt;/P&gt;&lt;P&gt;for ADT 19MAY2025 which is not fall any date range , so 19AMAY2025 previous dose is 14 , so for this record 14 need to assign same for 15JUN2025 previous dose is 18 , first need to assign between date range , if still missing then assign previous dose&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you,&lt;/P&gt;&lt;P&gt;Raja&lt;/P&gt;</description>
      <pubDate>Sat, 20 Sep 2025 00:47:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assing-Value-based-on-Dates-Range/m-p/975398#M378098</guid>
      <dc:creator>raja777pharma</dc:creator>
      <dc:date>2025-09-20T00:47:23Z</dc:date>
    </item>
    <item>
      <title>Re: Assing Value based on Dates Range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assing-Value-based-on-Dates-Range/m-p/975403#M378102</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data exdose;
length USUBJID $7;
format stdt endt date9.;
input USUBJID $ EXDOSE stdt :date9. endt :date9.;
datalines;
1361007 14 16MAY2025 18MAY2025
1361007 14 20MAY2025 30MAY2025
1361007 18 31MAY2025 14JUN2025
1361007 18 16JUN2025 18JUN2025
;
run;

 

data second_ds;
length USUBJID $7;
format adt date9.;
input USUBJID $  ADT :date9.;
datalines;
1361007 17MAY2025 
1361007 19MAY2025
1361007 30MAY2025
1361007 15JUN2025 
;
run;

proc sql;
create table part1 as
select a.*,b.EXDOSE
 from second_ds as a ,exdose as b
  where a.USUBJID=b.USUBJID and a.ADT between b.stdt and b.endt;

create table part2 as
select a.*,b.EXDOSE
 from (select * from second_ds where catx(' ',USUBJID,ADT) not in (select catx(' ',USUBJID,ADT) from part1)) as a,exdose as b
  where a.USUBJID=b.USUBJID and a.ADT &amp;gt; b.endt
   group by a.USUBJID,a.ADT
    having b.endt=max(b.endt);

create table want as
select * from part1
union 
select * from part2
order by USUBJID,ADT;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 20 Sep 2025 07:45:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assing-Value-based-on-Dates-Range/m-p/975403#M378102</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2025-09-20T07:45:41Z</dc:date>
    </item>
    <item>
      <title>Re: Assing Value based on Dates Range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assing-Value-based-on-Dates-Range/m-p/975405#M378103</link>
      <description>How many observations do your datasets contain?</description>
      <pubDate>Sat, 20 Sep 2025 11:11:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assing-Value-based-on-Dates-Range/m-p/975405#M378103</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2025-09-20T11:11:53Z</dc:date>
    </item>
    <item>
      <title>Re: Assing Value based on Dates Range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assing-Value-based-on-Dates-Range/m-p/975407#M378104</link>
      <description>&lt;P&gt;Here's an alternative.&amp;nbsp; I added another record at the beginning of SECOND_DS just to make sure that it was working correctly - for ADT=10MAY, there are no doses available yet from EXDOSE, so that row ends up with missing values.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data exdose;
length USUBJID $7;
format stdt endt date9.;
input USUBJID $ EXDOSE stdt :date9. endt :date9.;
datalines;
1361007 14 16MAY2025 18MAY2025
1361007 14 20MAY2025 30MAY2025
1361007 18 31MAY2025 14JUN2025
1361007 18 16JUN2025 18JUN2025
;
run;

data second_ds;
length USUBJID $7;
format adt date9.;
input USUBJID $  ADT :date9.;
datalines;
1361007 10MAY2025
1361007 17MAY2025 
1361007 19MAY2025
1361007 30MAY2025
1361007 15JUN2025 
;
run;

proc sql noprint;
select max(nrecs) into :maxrecs trimmed
from (select usubjid, count(*) as nrecs from exdose group by usubjid);
quit;

data want;
set
	exdose
	second_ds (in=B)
	;
sd=B;
run;

proc sort data=want; by usubjid sd stdt adt; run;

data want;
set want;
by usubjid;
array T {0:&amp;amp;maxrecs,3} _temporary_;
retain j;
if first.usubjid then do;
	call missing(of T[*], i);
	j=0;
end;
if not sd then do;
	i+1;
	T[i,1]=stdt;
	T[i,2]=endt;
	T[i,3]=exdose;
end;
else do;
	do while (adt&amp;gt;T[j,2] and j&amp;lt;i);
		j+1;
	end;
	if adt&amp;lt;T[j,1] then j=j-1;
	stdt=T[j,1];
	endt=T[j,2];
	exdose=T[j,3];
	output;
end;
keep usubjid stdt endt exdose adt;
run;

proc print data=want; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;RESULT:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="quickbluefish_0-1758375616613.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/110068i3F7146A3EE207822/image-size/medium?v=v2&amp;amp;px=400" role="button" title="quickbluefish_0-1758375616613.png" alt="quickbluefish_0-1758375616613.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 20 Sep 2025 14:09:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assing-Value-based-on-Dates-Range/m-p/975407#M378104</guid>
      <dc:creator>quickbluefish</dc:creator>
      <dc:date>2025-09-20T14:09:12Z</dc:date>
    </item>
    <item>
      <title>Re: Assing Value based on Dates Range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assing-Value-based-on-Dates-Range/m-p/975414#M378107</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/309000"&gt;@raja777pharma&lt;/a&gt;&amp;nbsp;, it looks to me like you have two datasets, one of which is the exposure dosage (&lt;EM&gt;exdose&lt;/EM&gt;) data, which plans to administer two dosage of treatment (14mg and 18mg, twice of each) to a subject on a certain date, and the date range is determined. In the other dataset, a disposition (&lt;EM&gt;second_ds&lt;/EM&gt;) dataset, the date (i.e., the&lt;EM&gt; adt&lt;/EM&gt;) for administer the treatment changed (or had adjustment) and two of the &lt;EM&gt;adt&lt;/EM&gt; date are not in range, so for that two &lt;EM&gt;adt&lt;/EM&gt; date, the dosage should be the one which was planned (in &lt;EM&gt;exdose&lt;/EM&gt; dataset) to assign to the time range before that &lt;EM&gt;adt&lt;/EM&gt; date. My code and output is as follows, and the result is the same as&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/223320"&gt;@quickbluefish&lt;/a&gt;&amp;nbsp;'s (and a while later I found my steps in fact are the same with &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;'s, except my steps is more basic and simple and &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;combined all &lt;EM&gt;proc sql&lt;/EM&gt; steps in one and use more advanced technique), please kindly let me know if I answered your question, thanks a lot!&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data exdose;
   input USUBJID $ EXDOSE 
         stdt :date9. endt :date9.;
   format stdt endt date9.;
   datalines;
1361007 14 16MAY2025 18MAY2025
1361007 14 20MAY2025 30MAY2025
1361007 18 31MAY2025 14JUN2025
1361007 18 16JUN2025 18JUN2025
;
run;
proc print data=exdose;run;
data second_ds;
   input USUBJID $  ADT date9.;
   format adt date9.;
datalines;
1361007 17MAY2025 
1361007 19MAY2025
1361007 30MAY2025
1361007 15JUN2025 
;
run;
proc print data=second_ds;run;
/*if adt date in the second_ds dataset 
is not in range, according to request, 
find the earliest date and last date
of the exposure dosage, and produce
a dataset that has two date ranges*/
proc sql;
create table t_range as
select usubjid,
       exdose,
       min(stdt) as stdtmin 
                 format=date9.,
       max(endt) as endtmax 
                 format=date9.
   from exdose
   group by usubjid,
            exdose;
select * from t_range;
quit;
proc sql;
create table t_range2 as
select ed.*,
       tr.stdtmin,
       tr.endtmax
   from exdose as ed left join
        t_range as tr
   on ed.usubjid=tr.usubjid and
      ed.exdose=tr.exdose;
select * from t_range2;
quit;
/*determine which date range the adt date
belongs to, and assign the correspondence
dosage to the adt date*/
proc sql;
create table final1 as
select ds2.*,
       tr2.exdose
   from second_ds as ds2 left join
        t_range2 as tr2
   on ds2.usubjid=tr2.usubjid
   where tr2.stdt&amp;lt;=ds2.adt&amp;lt;=tr2.endt;
select * from final1;
quit;
proc sql;
create table final2 as
select ds2.*,
       tr2.exdose
   from second_ds as ds2 left join
        t_range2 as tr2
   on ds2.usubjid=tr2.usubjid
   where tr2.stdtmin&amp;lt;=ds2.adt&amp;lt;=tr2.endtmax
except
select ds2.*,
       tr2.exdose
   from second_ds as ds2 left join
        t_range2 as tr2
   on ds2.usubjid=tr2.usubjid
   where tr2.stdt&amp;lt;=ds2.adt&amp;lt;=tr2.endt;
select * from final2;
quit;
proc sql;
create table ds2_exdose as
select * from final1
union
select * from final2
order by usubjid,adt;
select * from ds2_exdose;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dxiao2017_0-1758384236469.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/110069i3563543DE5952DE6/image-size/large?v=v2&amp;amp;px=999" role="button" title="dxiao2017_0-1758384236469.png" alt="dxiao2017_0-1758384236469.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dxiao2017_1-1758384318954.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/110070i6F799408DF46588B/image-size/large?v=v2&amp;amp;px=999" role="button" title="dxiao2017_1-1758384318954.png" alt="dxiao2017_1-1758384318954.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dxiao2017_2-1758384383378.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/110071iD310A1429DA62001/image-size/large?v=v2&amp;amp;px=999" role="button" title="dxiao2017_2-1758384383378.png" alt="dxiao2017_2-1758384383378.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 20 Sep 2025 16:19:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assing-Value-based-on-Dates-Range/m-p/975414#M378107</guid>
      <dc:creator>dxiao2017</dc:creator>
      <dc:date>2025-09-20T16:19:14Z</dc:date>
    </item>
    <item>
      <title>Re: Assing Value based on Dates Range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assing-Value-based-on-Dates-Range/m-p/975415#M378108</link>
      <description>Hi Kurt_Bremser, I guess the dataset won't be big and does not have too many observations because it looks like the datasets are from phase I or II or it is pk/pd dataset, the sample is small</description>
      <pubDate>Sat, 20 Sep 2025 18:35:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assing-Value-based-on-Dates-Range/m-p/975415#M378108</guid>
      <dc:creator>dxiao2017</dc:creator>
      <dc:date>2025-09-20T18:35:22Z</dc:date>
    </item>
    <item>
      <title>Re: Assing Value based on Dates Range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assing-Value-based-on-Dates-Range/m-p/975436#M378116</link>
      <description>&lt;P&gt;If you just want to remember the previous dose then you might consider just interleaving the observations instead trying to use SQL joins or data step MERGEs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Setup datasets:&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data exdose;
  input USUBJID :$7. EXDOSE stdt :date. endt :date.;
  format stdt endt date9.;
datalines;
1361007 14 16MAY2025 18MAY2025
1361007 14 20MAY2025 30MAY2025
1361007 18 31MAY2025 14JUN2025
1361007 18 16JUN2025 18JUN2025
;

data second_ds;
  input USUBJID :$7. ADT :date.;
  format adt date9.;
datalines;
1361007 17MAY2025 
1361007 19MAY2025
1361007 30MAY2025
1361007 15JUN2025 
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI-SPOILER&gt;
&lt;P&gt;Interleave them and remember the EXDOSE value into a new variable that is retained.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set exdose(in=in1 keep=usubjid exdose stdt rename=(stdt=adt))
      second_ds(in=in2)
  ;
  by usubjid adt;
  if in1 then dose=exdose;
  if in2 then output;
  if last.usubjid then call missing(dose);
  retain dose;
  drop exdose ;
  rename dose=exdose;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt;Obs    USUBJID          adt    exdose

 1     1361007    17MAY2025      14
 2     1361007    19MAY2025      14
 3     1361007    30MAY2025      14
 4     1361007    15JUN2025      18
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 21 Sep 2025 22:09:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assing-Value-based-on-Dates-Range/m-p/975436#M378116</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-09-21T22:09:55Z</dc:date>
    </item>
    <item>
      <title>Re: Assing Value based on Dates Range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assing-Value-based-on-Dates-Range/m-p/975453#M378123</link>
      <description>&lt;P&gt;Hi Tom, thanks a lot for reply! I think this is very brilliant, I hope I can write code like this after a lot of practice. In fact, the key word &lt;EM&gt;set&lt;/EM&gt;, &lt;EM&gt;retain&lt;/EM&gt;, and &lt;EM&gt;if.first/last&lt;/EM&gt; (and some other techniques that do not produce a visible output or a product that directly show how they work) are the techniques that I use least because I have not fully understand the logic (how they work) of them. It is good learning experience for me which let me know how to improve my code and thinking.&lt;/P&gt;</description>
      <pubDate>Mon, 22 Sep 2025 05:32:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assing-Value-based-on-Dates-Range/m-p/975453#M378123</guid>
      <dc:creator>dxiao2017</dc:creator>
      <dc:date>2025-09-22T05:32:09Z</dc:date>
    </item>
  </channel>
</rss>

