<?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 How can I dynamically reference a variable in calculations? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-dynamically-reference-a-variable-in-calculations/m-p/994097#M380505</link>
    <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am working with patient visits data. Each row is a different visit. A patient can have any number of visits, and visits are still ongoing.&lt;/P&gt;&lt;PRE&gt;data pt_visits;&lt;BR /&gt;INPUT patient $ visit_date :MMDDYY10.;&lt;BR /&gt;FORMAT visit_date MMDDYY10.;&lt;BR /&gt;datalines;&lt;BR /&gt;A 01/12/2026&lt;BR /&gt;A 01/16/2026&lt;BR /&gt;A 02/04/2026&lt;BR /&gt;B 01/05/2026&lt;BR /&gt;B 01/09/2026&lt;BR /&gt;B 01/10/2026&lt;BR /&gt;B 01/18/2026&lt;BR /&gt;B 01/31/2026&lt;BR /&gt;B 02/05/2026&lt;BR /&gt;;&lt;BR /&gt;run;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Many of the metrics I'm reporting are at the patient level, so I've transposed the data:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=pt_visits; by patient visit_date; run;

/*count patient visits*/
data pt_visits_ct;
	set pt_visits;
	by patient;
	if first.patient then visit_ct=0;
	visit_ct+1;
run;

/*one row per patient*/
proc transpose data=pt_visits_ct out=pt_visits_wide prefix=visit;
	by patient;
	id visit_ct;
	var visit_date;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I need to calculate things such as most recent visit and the time that has elapsed since the most recent visit:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data calc;
	set pt_visits_wide;
	last_visit=max(of visit1--&lt;FONT color="#FF0000"&gt;visit6&lt;/FONT&gt;);
	days_since_last_visit=today()-max(of visit1--&lt;FONT color="#FF0000"&gt;visit6&lt;/FONT&gt;);
	format last_visit mmddyy10.;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;My code above uses visit6 in calculations because Patient B, with the most visits, has 6 visits. However, as time goes on and patients have more visits, visit6 will go on to become visit7, then visit8, etc. How can I reference visitn, with n being the number of visits that the patient with the most visits has?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 24 Sep 2026 19:09:08 GMT</pubDate>
    <dc:creator>bonc826</dc:creator>
    <dc:date>2026-09-24T19:09:08Z</dc:date>
    <item>
      <title>How can I dynamically reference a variable in calculations?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-dynamically-reference-a-variable-in-calculations/m-p/994097#M380505</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am working with patient visits data. Each row is a different visit. A patient can have any number of visits, and visits are still ongoing.&lt;/P&gt;&lt;PRE&gt;data pt_visits;&lt;BR /&gt;INPUT patient $ visit_date :MMDDYY10.;&lt;BR /&gt;FORMAT visit_date MMDDYY10.;&lt;BR /&gt;datalines;&lt;BR /&gt;A 01/12/2026&lt;BR /&gt;A 01/16/2026&lt;BR /&gt;A 02/04/2026&lt;BR /&gt;B 01/05/2026&lt;BR /&gt;B 01/09/2026&lt;BR /&gt;B 01/10/2026&lt;BR /&gt;B 01/18/2026&lt;BR /&gt;B 01/31/2026&lt;BR /&gt;B 02/05/2026&lt;BR /&gt;;&lt;BR /&gt;run;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Many of the metrics I'm reporting are at the patient level, so I've transposed the data:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=pt_visits; by patient visit_date; run;

/*count patient visits*/
data pt_visits_ct;
	set pt_visits;
	by patient;
	if first.patient then visit_ct=0;
	visit_ct+1;
run;

/*one row per patient*/
proc transpose data=pt_visits_ct out=pt_visits_wide prefix=visit;
	by patient;
	id visit_ct;
	var visit_date;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I need to calculate things such as most recent visit and the time that has elapsed since the most recent visit:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data calc;
	set pt_visits_wide;
	last_visit=max(of visit1--&lt;FONT color="#FF0000"&gt;visit6&lt;/FONT&gt;);
	days_since_last_visit=today()-max(of visit1--&lt;FONT color="#FF0000"&gt;visit6&lt;/FONT&gt;);
	format last_visit mmddyy10.;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;My code above uses visit6 in calculations because Patient B, with the most visits, has 6 visits. However, as time goes on and patients have more visits, visit6 will go on to become visit7, then visit8, etc. How can I reference visitn, with n being the number of visits that the patient with the most visits has?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Sep 2026 19:09:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-dynamically-reference-a-variable-in-calculations/m-p/994097#M380505</guid>
      <dc:creator>bonc826</dc:creator>
      <dc:date>2026-09-24T19:09:08Z</dc:date>
    </item>
    <item>
      <title>Re: How can I dynamically reference a variable in calculations?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-dynamically-reference-a-variable-in-calculations/m-p/994158#M380506</link>
      <description>&lt;P&gt;Your original data structure is probably best for determining "most recent" visit. Sort the data by patient id and descending visit date. The first visit will then be the most recent.&lt;/P&gt;
&lt;P&gt;Intervals as well using the Intck and Retain functions.&lt;/P&gt;
&lt;PRE&gt;Proc sort data=Pt_visits out=sorted_visits;
   by patient descending visit_date;
run;

data visit_interval;
   set sorted_visits;
   by patient;
   retain most_recent;
   format most_recent mmddyy10.;
   if first.patient then do;
      most_recent=visit_date;
      date_interval=0;
   end;
   else  date_interval = intck('day',visit_date,most_recent);
   /* drop most_recent;*/
run;&lt;/PRE&gt;
&lt;P&gt;RETAIN keeps the value of the specified variable(s) across the data step boundary until reset. In this case using BY patient allows us to use the FIRST.&amp;lt;by variable name&amp;gt; construct to identify the first patient record and set the most_recent visit date from the sorted values.&lt;/P&gt;
&lt;P&gt;INTCK yields interval counts between two date variables. The order of the the two dates is important. Please read the documentation. Date and/or time intervals are usually best worked with the functions INTCK, for returning interval between, and INTNX to increment dates. You can get intervals such as week, month, quarter or year (and others) for example as needed and let the staff at SAS do the background programming.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Unless there is something else needed then the VisitN variable isn't needed though could be build in the step that uses the sorted data.&lt;/P&gt;
&lt;P&gt;Note that if multiple FACILITIES are to be used then you may want to sort by patient and facility and use similar logic using First.Facility to hand the most recent and interval per patient facility combination if needed. Or a treatment code, doctor or similar seen by the patient, etc.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Uncomment the Drop statement if the most_recent visit variable is not needed later.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Often making "wide" data such as your transposes is not needed.&lt;/P&gt;
&lt;P&gt;If you are careful with your variable names you can use&amp;nbsp; MAX (of varname: ) which will use all of the variables whose names start with VARNAME . So if the only variable starting with listed characters are what you need the : list indentifier simplifies the code and will handle the results of Proc Transpose output without having to change the code.&lt;/P&gt;</description>
      <pubDate>Sat, 26 Sep 2026 01:39:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-dynamically-reference-a-variable-in-calculations/m-p/994158#M380506</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2026-09-26T01:39:08Z</dc:date>
    </item>
    <item>
      <title>Re: How can I dynamically reference a variable in calculations?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-dynamically-reference-a-variable-in-calculations/m-p/994162#M380507</link>
      <description>&lt;P&gt;Maybe I don't understand your question completely.&lt;/P&gt;
&lt;P&gt;You could use colon operator to represent all the variables which starts with VISIT. Like&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data calc;
	set pt_visits_wide;
	last_visit=max(of &lt;STRONG&gt;visit:&lt;/STRONG&gt;);
	days_since_last_visit=today()-max(of &lt;STRONG&gt;visit:&lt;/STRONG&gt;);
	format last_visit mmddyy10.;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 26 Sep 2026 07:11:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-dynamically-reference-a-variable-in-calculations/m-p/994162#M380507</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2026-09-26T07:11:11Z</dc:date>
    </item>
    <item>
      <title>Re: How can I dynamically reference a variable in calculations?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-dynamically-reference-a-variable-in-calculations/m-p/994164#M380508</link>
      <description>&lt;P&gt;Or could be more succinct code by SQL.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data pt_visits;
INPUT patient $ visit_date :MMDDYY10.;
FORMAT visit_date MMDDYY10.;
datalines;
A 01/12/2026
A 01/16/2026
A 02/04/2026
B 01/05/2026
B 01/09/2026
B 01/10/2026
B 01/18/2026
B 01/31/2026
B 02/05/2026
;
run;

proc sql;
create table calc as
select patient,max(visit_date) as last_visit format=mmddyy10.,today()-calculated last_visit as days_since_last_visit
 from pt_visits
  group by patient
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 26 Sep 2026 07:17:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-dynamically-reference-a-variable-in-calculations/m-p/994164#M380508</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2026-09-26T07:17:13Z</dc:date>
    </item>
    <item>
      <title>Re: How can I dynamically reference a variable in calculations?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-dynamically-reference-a-variable-in-calculations/m-p/994166#M380509</link>
      <description>&lt;P&gt;Strongly agreeing with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;who said:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;Often making "wide" data such as your transposes is not needed.&lt;/P&gt;
&lt;P&gt;If you are careful with your variable names you can use MAX (of varname: ) which will use all of the variables whose names start with VARNAME . So if the only variable starting with listed characters are what you need the : list indentifier simplifies the code and will handle the results of Proc Transpose output without having to change the code.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Rarely is transposing the best solution, and it makes your programming more difficult.&lt;/P&gt;</description>
      <pubDate>Sat, 26 Sep 2026 10:20:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-dynamically-reference-a-variable-in-calculations/m-p/994166#M380509</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2026-09-26T10:20:42Z</dc:date>
    </item>
  </channel>
</rss>

