<?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: loop for changing variables in changing datasets in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/loop-for-changing-variables-in-changing-datasets/m-p/881161#M348181</link>
    <description>&lt;P&gt;At this point I think I am going call "Foul" on use of the word "median" at all to describe this process.&lt;/P&gt;
&lt;P&gt;When I use that example data and run the code the result I get is&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Obs     Eink                  Bg                 He

 1     Median    1526.8329804346    2278.1399438082


&lt;/PRE&gt;
&lt;P&gt;Every definition I have run into for median relates to "being in the middle" of values with some slop as to exactly how that middle is defined. Or perhaps "Formally, a median of a &lt;A class="mw-redirect" title="Population (statistics)" href="https://en.wikipedia.org/wiki/Population_(statistics)" target="_blank"&gt;population&lt;/A&gt; is any value such that at least half of the population is less than or equal to the proposed median and at least half is greater than or equal to the proposed median."&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since the largest value in your Final example data for Bg is 461.6179330 I see no way that a value of 1526 or greater should be considered any sort of "median" for Bg. Similar with your He variable as the max in the set is 192.283645.&lt;/P&gt;
&lt;P&gt;This may be a statistic of some sort but a median it ain't.&lt;/P&gt;</description>
    <pubDate>Fri, 16 Jun 2023 18:12:58 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2023-06-16T18:12:58Z</dc:date>
    <item>
      <title>loop for changing variables in changing datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/loop-for-changing-variables-in-changing-datasets/m-p/880602#M347939</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a problem and Im thankful for help.&lt;/P&gt;
&lt;P&gt;Given is the data set "final". Today, for example, it contains 3 variables, but it could also have 5 or 10 variables the next time it is created. The names of the variables are always freely selectable and have no logic. But they all have one thing in common: The first variable is always "Eink" with the same characteristics and the goal is calculating a median (based on the groups in "Eink").&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For the current dataset I have written a syntax for calculating the median. I attached the current syntax below. Based on my current dataset, &lt;STRONG&gt;Bg&lt;/STRONG&gt; and &lt;STRONG&gt;He&lt;/STRONG&gt; were used there.&amp;nbsp; My problem is: If the record "final" appears tomorrow with other variables, the syntax should adapt individually to it. If tomorrow "Bg" and "He" no longer exist, but for example XX, XY and XZ, then the syntax should automatically be applied to these three variables with their specific names. So I probably need a loop, but am unable to write it. Could anyone help me with this big concern? Would be great!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;dataset "final" given&lt;/STRONG&gt;&lt;/P&gt;
&lt;TABLE style="border-collapse: collapse; width: 180pt;" border="0" width="289px" cellspacing="0" cellpadding="0"&gt;
&lt;TBODY&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="94.9333px" height="15px" style="height: 15.0pt; width: 60pt;"&gt;Eink&lt;/TD&gt;
&lt;TD width="97.6167px" height="15px" style="width: 60pt;"&gt;Bg&lt;/TD&gt;
&lt;TD width="96.1167px" height="15px" style="width: 60pt;"&gt;He&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="94.9333px" height="15px" style="height: 15.0pt; width: 60pt;"&gt;01&lt;/TD&gt;
&lt;TD width="97.6167px" height="15px"&gt;55.487739&lt;/TD&gt;
&lt;TD width="96.1167px" height="15px"&gt;2.722371&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="94.9333px" height="15px" style="height: 15.0pt;"&gt;02&lt;/TD&gt;
&lt;TD width="97.6167px" height="15px"&gt;127.485645&lt;/TD&gt;
&lt;TD width="96.1167px" height="15px"&gt;18.670678&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="94.9333px" height="15px" style="height: 15.0pt;"&gt;03&lt;/TD&gt;
&lt;TD width="97.6167px" height="15px"&gt;182.95309&lt;/TD&gt;
&lt;TD width="96.1167px" height="15px"&gt;53.529988&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="94.9333px" height="15px" style="height: 15.0pt;"&gt;04&lt;/TD&gt;
&lt;TD width="97.6167px" height="15px"&gt;354.69612&lt;/TD&gt;
&lt;TD width="96.1167px" height="15px"&gt;122.703093&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="94.9333px" height="15px" style="height: 15.0pt;"&gt;05&lt;/TD&gt;
&lt;TD width="97.6167px" height="15px"&gt;450.777081&lt;/TD&gt;
&lt;TD width="96.1167px" height="15px"&gt;137.596942&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="94.9333px" height="15px" style="height: 15.0pt;"&gt;06&lt;/TD&gt;
&lt;TD width="97.6167px" height="15px"&gt;461.617933&lt;/TD&gt;
&lt;TD width="96.1167px" height="15px"&gt;157.457164&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="94.9333px" height="15px" style="height: 15.0pt;"&gt;07&lt;/TD&gt;
&lt;TD width="97.6167px" height="15px"&gt;429.38197&lt;/TD&gt;
&lt;TD width="96.1167px" height="15px"&gt;192.283645&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="94.9333px" height="15px" style="height: 15.0pt;"&gt;08&lt;/TD&gt;
&lt;TD width="97.6167px" height="15px"&gt;370.644235&lt;/TD&gt;
&lt;TD width="96.1167px" height="15px"&gt;173.688658&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="94.9333px" height="15px" style="height: 15.0pt;"&gt;09&lt;/TD&gt;
&lt;TD width="97.6167px" height="15px"&gt;281.256479&lt;/TD&gt;
&lt;TD width="96.1167px" height="15px"&gt;180.848677&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="94.9333px" height="15px" style="height: 15.0pt;"&gt;10&lt;/TD&gt;
&lt;TD width="97.6167px" height="15px"&gt;170.426973&lt;/TD&gt;
&lt;TD width="96.1167px" height="15px"&gt;137.045263&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="94.9333px" height="15px" style="height: 15.0pt;"&gt;11&lt;/TD&gt;
&lt;TD width="97.6167px" height="15px"&gt;118.22022&lt;/TD&gt;
&lt;TD width="96.1167px" height="15px"&gt;128.45571&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="94.9333px" height="15px" style="height: 15.0pt;"&gt;12&lt;/TD&gt;
&lt;TD width="97.6167px" height="15px"&gt;93.656461&lt;/TD&gt;
&lt;TD width="96.1167px" height="15px"&gt;104.596442&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="94.9333px" height="15px" style="height: 15.0pt;"&gt;13&lt;/TD&gt;
&lt;TD width="97.6167px" height="15px"&gt;68.561622&lt;/TD&gt;
&lt;TD width="96.1167px" height="15px"&gt;112.710123&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="94.9333px" height="15px" style="height: 15.0pt;"&gt;14&lt;/TD&gt;
&lt;TD width="97.6167px" height="15px"&gt;42.443895&lt;/TD&gt;
&lt;TD width="96.1167px" height="15px"&gt;77.742469&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="94.9333px" height="15px" style="height: 15.0pt;"&gt;15&lt;/TD&gt;
&lt;TD width="97.6167px" height="15px"&gt;50.933162&lt;/TD&gt;
&lt;TD width="96.1167px" height="15px"&gt;136.718895&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="94.9333px" height="15px" style="height: 15.0pt;"&gt;16&lt;/TD&gt;
&lt;TD width="97.6167px" height="15px"&gt;35.285987&lt;/TD&gt;
&lt;TD width="96.1167px" height="15px"&gt;104.219131&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="94.9333px" height="15px" style="height: 15.0pt;"&gt;17&lt;/TD&gt;
&lt;TD width="97.6167px" height="15px"&gt;23.123863&lt;/TD&gt;
&lt;TD width="96.1167px" height="15px"&gt;78.424041&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="94.9333px" height="15px" style="height: 15.0pt;"&gt;18&lt;/TD&gt;
&lt;TD width="97.6167px" height="15px"&gt;16.724659&lt;/TD&gt;
&lt;TD width="96.1167px" height="15px"&gt;89.48717&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="94.9333px" height="15px" style="height: 15.0pt;"&gt;19&lt;/TD&gt;
&lt;TD width="97.6167px" height="15px"&gt;9.271308&lt;/TD&gt;
&lt;TD width="96.1167px" height="15px"&gt;46.396113&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="94.9333px" height="15px" style="height: 15.0pt;"&gt;20&lt;/TD&gt;
&lt;TD width="97.6167px" height="15px"&gt;4.870804&lt;/TD&gt;
&lt;TD width="96.1167px" height="15px"&gt;21.574738&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="94.9333px" height="15px" style="height: 15.0pt;"&gt;21&lt;/TD&gt;
&lt;TD width="97.6167px" height="15px"&gt;4.83902&lt;/TD&gt;
&lt;TD width="96.1167px" height="15px"&gt;19.753305&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="94.9333px" height="15px" style="height: 15.0pt;"&gt;22&lt;/TD&gt;
&lt;TD width="97.6167px" height="15px"&gt;3.487616&lt;/TD&gt;
&lt;TD width="96.1167px" height="15px"&gt;9.23105&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="94.9333px" height="15px" style="height: 15.0pt;"&gt;23&lt;/TD&gt;
&lt;TD width="97.6167px" height="15px"&gt;0.860551&lt;/TD&gt;
&lt;TD width="96.1167px" height="15px"&gt;2.136266&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="94.9333px" height="15px" align="right" class="lia-align-left" style="height: 15.0pt;"&gt;24&lt;/TD&gt;
&lt;TD width="97.6167px" height="15px"&gt;1.202043&lt;/TD&gt;
&lt;TD width="96.1167px" height="15px"&gt;1.861731&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;code&lt;/STRONG&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
/*1*/

Data Eink_2;
set final;
if Eink = "01" then do; 	Xmin = 	0		; Xmax = 	250		; end;
if Eink = "02" then do; 	Xmin = 	250		; Xmax = 	500		; end;
if Eink = "03" then do; 	Xmin = 	500		; Xmax = 	750		; end;
if Eink = "04" then do; 	Xmin = 	750		; Xmax = 	1000	; end;
if Eink = "05" then do; 	Xmin = 	1000	; Xmax = 	1250	; end;
if Eink = "06" then do; 	Xmin = 	1250	; Xmax = 	1500	; end;
if Eink = "07" then do; 	Xmin = 	1500	; Xmax = 	1750	; end;
if Eink = "08" then do; 	Xmin = 	1750	; Xmax = 	2000	; end;
if Eink = "09" then do; 	Xmin = 	2000	; Xmax = 	2250	; end;
if Eink = "10" then do; 	Xmin = 	2250	; Xmax = 	2500	; end;
if Eink = "11" then do; 	Xmin = 	2500	; Xmax = 	2750	; end;
if Eink = "12" then do; 	Xmin = 	2750	; Xmax = 	3000	; end;
if Eink = "13" then do; 	Xmin = 	3000	; Xmax = 	3250	; end;
if Eink = "14" then do; 	Xmin = 	3250	; Xmax = 	3500	; end;
if Eink = "15" then do; 	Xmin = 	3500	; Xmax = 	4000	; end;
if Eink = "16" then do; 	Xmin = 	4000	; Xmax = 	4500	; end;
if Eink = "17" then do; 	Xmin = 	4500	; Xmax = 	5000	; end;
if Eink = "18" then do; 	Xmin = 	5000	; Xmax = 	6000	; end;
if Eink = "19" then do; 	Xmin = 	6000	; Xmax = 	7000	; end;
if Eink = "20" then do; 	Xmin = 	7000	; Xmax = 	8000	; end;
if Eink = "21" then do; 	Xmin = 	8000	; Xmax = 	10000	; end;
if Eink = "22" then do; 	Xmin = 	10000	; Xmax = 	15000	; end;
if Eink = "23" then do; 	Xmin = 	15000	; Xmax = 	25000	; end;
if Eink = "24" then do; 	Xmin = 	25000	; Xmax = 	.		; end;
run;

/*2*/

Data Eink_3;
set Eink_2;
cum_Bg+Bg;
cum_He+He;
run;


/*3*/

proc sql noprint;
select sum(Bg)/2 		into: n2_B	from Eink_3;
select sum(He)/2 		into: n2_H	from Eink_3;
run;



/*4*/

data Eink_4;
set Eink_3;
Fx_prä_B = 	&amp;amp;n2_B. - cum_Bg;
Fx_prä_H = 	&amp;amp;n2_H. - cum_He;
run;


/*5*/

	proc rank data=Eink_4 out=fx_1_Rang_B; 	var Fx_prä_B; 	ranks rang ; where Fx_prä_B &amp;gt;0;
	proc rank data=Eink_4 out=fx_1_Rang_H; 	var Fx_prä_H; 	ranks rang ; where Fx_prä_H &amp;gt;0;


/*6*/

	proc sql noprint; select put(cum_Bg, decimal.7) 	into:Fx_1_B from fx_1_Rang_B 	where rang =1; quit;
	proc sql noprint; select put(cum_He, decimal.7) 	into:Fx_1_H from fx_1_Rang_H 	where rang =1; quit;



/*7*/

	proc rank data=Eink_4 descending out=fx_B; 	var Fx_prä_B; 	ranks Rang; where Fx_prä_B &amp;lt;0;
	proc rank data=Eink_4 descending out=fx_H; 	var Fx_prä_H; 	ranks Rang; where Fx_prä_H &amp;lt;0;


/*8*/

	proc sql noprint;
	select put(Bg, decimal.7) 	into:fx_B 	from fx_B where rang=1;
	select put(He, decimal.7) 	into:fx_H 	from fx_H where rang=1;
	run;


/*9*/

proc sql noprint;
select Xmin into: Xmin_B from fx_B where Rang = 1;
select Xmin into: Xmin_H from fx_H where Rang = 1;
quit;


/*10*/

proc sql noprint;
select Xmax-Xmin into: b_B 	from fx_B 	where Rang = 1;
select Xmax-Xmin into: b_H 	from fx_H 	where Rang = 1;
quit;



/*11 - into the formula */

Data Median_Final;
retain Eink Bg He;
Format 
Bg 15.12
He 15.12;
Eink 	= "Median";
Bg =	&amp;amp;Xmin_B.  	+ &amp;amp;b_B.*((&amp;amp;n2_B.-&amp;amp;Fx_1_B.)/&amp;amp;fx_B.);
He 	=	&amp;amp;Xmin_H.  	+ &amp;amp;b_H.*((&amp;amp;n2_H.-&amp;amp;Fx_1_H.)/&amp;amp;fx_H.);
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Jun 2023 10:39:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/loop-for-changing-variables-in-changing-datasets/m-p/880602#M347939</guid>
      <dc:creator>Konkordanz</dc:creator>
      <dc:date>2023-06-14T10:39:26Z</dc:date>
    </item>
    <item>
      <title>Re: loop for changing variables in changing datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/loop-for-changing-variables-in-changing-datasets/m-p/880604#M347941</link>
      <description>&lt;P&gt;Transpose to long, so you can use the _NAME_ variable as additional CLASS in PROC MEANS or ACROSS in PROC REPORT. If eink is the only character variable, you can use _NUMERIC_ in the VAR statement of TRANSPOSE.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Jun 2023 09:54:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/loop-for-changing-variables-in-changing-datasets/m-p/880604#M347941</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-06-14T09:54:45Z</dc:date>
    </item>
    <item>
      <title>Re: loop for changing variables in changing datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/loop-for-changing-variables-in-changing-datasets/m-p/880606#M347942</link>
      <description>&lt;P&gt;Hmm, wouldnt be the best idea, because I have a total of 11 steps, which each create their own temporary datasets. As an example, I'll supplement my code snippet from above with the subsequent step of my syntax. So they always refer to each other. &lt;/P&gt;
&lt;P&gt;I would like to apply these 11 steps as a loop. The code snippet was just one of these 11 on which the macro could be exemplified. I'll try to include the rest then.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Jun 2023 10:12:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/loop-for-changing-variables-in-changing-datasets/m-p/880606#M347942</guid>
      <dc:creator>Konkordanz</dc:creator>
      <dc:date>2023-06-14T10:12:52Z</dc:date>
    </item>
    <item>
      <title>Re: loop for changing variables in changing datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/loop-for-changing-variables-in-changing-datasets/m-p/880607#M347943</link>
      <description>&lt;P&gt;Yes, please show us, because the suggestion from &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt; ought to work on the 11 different steps.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Jun 2023 10:22:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/loop-for-changing-variables-in-changing-datasets/m-p/880607#M347943</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-06-14T10:22:02Z</dc:date>
    </item>
    <item>
      <title>Re: loop for changing variables in changing datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/loop-for-changing-variables-in-changing-datasets/m-p/880609#M347944</link>
      <description>&lt;P&gt;I have fleshed out my initial post above and added my syntax that works based on the current data set (but I have reformulated/anonymized the syntax a bit). I hope this doesn't make too much work. Thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 14 Jun 2023 10:41:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/loop-for-changing-variables-in-changing-datasets/m-p/880609#M347944</guid>
      <dc:creator>Konkordanz</dc:creator>
      <dc:date>2023-06-14T10:41:37Z</dc:date>
    </item>
    <item>
      <title>Re: loop for changing variables in changing datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/loop-for-changing-variables-in-changing-datasets/m-p/880614#M347946</link>
      <description>&lt;P&gt;It looks like you are computing some sort of a median, using sums, max, min for each "half" of the data, and then plugging these into a formula. I would think there are better/faster ways to do this that don't involve macros or macro variables, but I don't fully understand the process or the end result. Could you please explain what you are trying to do?&lt;/P&gt;</description>
      <pubDate>Wed, 14 Jun 2023 11:38:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/loop-for-changing-variables-in-changing-datasets/m-p/880614#M347946</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-06-14T11:38:56Z</dc:date>
    </item>
    <item>
      <title>Re: loop for changing variables in changing datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/loop-for-changing-variables-in-changing-datasets/m-p/880707#M347975</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/388612"&gt;@Konkordanz&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hmm, wouldnt be the best idea, because I have a total of 11 steps, which each create their own temporary datasets. As an example, I'll supplement my code snippet from above with the subsequent step of my syntax. So they always refer to each other.&lt;/P&gt;
&lt;P&gt;I would like to apply these 11 steps as a loop. The code snippet was just one of these 11 on which the macro could be exemplified. I'll try to include the rest then.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If you have a data set with a grouping variable and want to create summary statistics for numeric variables then proc means/summary is likely where you belong. Splitting data into separate data sets just to get a median of a value is just "wrong" in a SAS approach.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is a small example with a data set you should have available to examine.&lt;/P&gt;
&lt;PRE&gt;Proc summary data=sashelp.class nway;
   class sex;
   var _numeric_;
   output out=want (drop=_type_ _freq_)
        median = ;
run;&lt;/PRE&gt;
&lt;P&gt;The option NWAY is to just show the result for the highest _type_ value of the result. _Type_ indicates which combination(s) of Class variables are involved in an output observation. The default output would also include an overall summary for all observations and if more variables are involved on the class statement then there multiple combinations possible.&lt;/P&gt;
&lt;P&gt;Class indicates your grouping variable.&lt;/P&gt;
&lt;P&gt;Var is list of numeric variables to use. In this case _numeric_ uses all of the numeric variables in the data set (there are only 3).&lt;/P&gt;
&lt;P&gt;The output statement names the output dataset and requests which statistic(s) to create. The drop removes two automatic variables that would appear by default. The Median= requests the median statistic. With a single statistic that means the output statistic has the same name as the input variable. You can either name the resulting statistic your self or use /autoname option to create names with the statistic appended.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Jun 2023 15:02:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/loop-for-changing-variables-in-changing-datasets/m-p/880707#M347975</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-06-14T15:02:34Z</dc:date>
    </item>
    <item>
      <title>Re: loop for changing variables in changing datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/loop-for-changing-variables-in-changing-datasets/m-p/880863#M348048</link>
      <description>&lt;P&gt;The goal is to calculate a weigthed median, based on a formular from the institution Im working for.&amp;nbsp; With the 11 steps I calculate each component of the formular:&lt;/P&gt;
&lt;P&gt;- Xmin_B and Xmin_H&lt;/P&gt;
&lt;P&gt;- b_B and b_H&lt;/P&gt;
&lt;P&gt;- n2_B and n2_H&lt;/P&gt;
&lt;P&gt;- Fx_1_B and Fx_1_H&lt;/P&gt;
&lt;P&gt;- fx_B and fx_H&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;At the end, I put these components into the formula and calculate the median (step 11). That works pretty well. But with new variables I always have to manually adjust the syntax. And my hope is, that there is a solution like a loop...&lt;/P&gt;</description>
      <pubDate>Thu, 15 Jun 2023 06:17:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/loop-for-changing-variables-in-changing-datasets/m-p/880863#M348048</guid>
      <dc:creator>Konkordanz</dc:creator>
      <dc:date>2023-06-15T06:17:11Z</dc:date>
    </item>
    <item>
      <title>Re: loop for changing variables in changing datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/loop-for-changing-variables-in-changing-datasets/m-p/881043#M348144</link>
      <description>Thx for answering. I wouldnt say "wrong", because I dont calculate an default median. I have to calculate a median based on a formular of the institution Im working for.  I have fleshed out my initial post above.</description>
      <pubDate>Fri, 16 Jun 2023 05:19:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/loop-for-changing-variables-in-changing-datasets/m-p/881043#M348144</guid>
      <dc:creator>Konkordanz</dc:creator>
      <dc:date>2023-06-16T05:19:40Z</dc:date>
    </item>
    <item>
      <title>Re: loop for changing variables in changing datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/loop-for-changing-variables-in-changing-datasets/m-p/881044#M348145</link>
      <description>&lt;P&gt;With such a complex process which can't use the available SAS procedures, you have to feed a list of variables to a macro and use %DO loops to create the repeating code wherever needed.&lt;/P&gt;
&lt;P&gt;You can create multiple macro variables in a single SELECT INTO, so use that to speed up your process.&lt;/P&gt;
&lt;P&gt;Similarly, avoid to run multiple steps for each variable; in most cases, you can process multiple variables in a single step.&lt;/P&gt;</description>
      <pubDate>Fri, 16 Jun 2023 05:38:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/loop-for-changing-variables-in-changing-datasets/m-p/881044#M348145</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-06-16T05:38:55Z</dc:date>
    </item>
    <item>
      <title>Re: loop for changing variables in changing datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/loop-for-changing-variables-in-changing-datasets/m-p/881151#M348177</link>
      <description>&lt;P&gt;Looks to me like your first 4 steps are just these two steps.&amp;nbsp; Plus I generated SIGN variables that can probably be used to reduce the PROC RANK steps to one instead of two.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data final ;
  input Eink :$2. Bg He ;
cards;
01 55.487739 2.722371
02 127.485645 18.670678
03 182.95309 53.529988
04 354.69612 122.703093
05 450.777081 137.596942
06 461.617933 157.457164
07 429.38197 192.283645
08 370.644235 173.688658
09 281.256479 180.848677
10 170.426973 137.045263
11 118.22022 128.45571
12 93.656461 104.596442
13 68.561622 112.710123
14 42.443895 77.742469
15 50.933162 136.718895
16 35.285987 104.219131
17 23.123863 78.424041
18 16.724659 89.48717
19 9.271308 46.396113
20 4.870804 21.574738
21 4.83902 19.753305
22 3.487616 9.23105
23 0.860551 2.136266
24 1.202043 1.861731
;

data min_max ;
  length Eink $2  Xmin Xmax 8;
  do xmax = 250 to 3500 by 250,4000,4500,5000,6000,7000,8000,10000,15000,25000,. ;
     i+1;
     eink=put(i,z2.);
     xmin=sum(0,lag(xmax));
     output;
  end;
  drop i;
run;

proc summary data=FINAL ;
  var Bg He ;
  output out=sums(drop=_freq_ _type_) sum=sum_Bg sum_He ;
run;

data STEP4;
  merge final min_max ;
  by eink;
  if _n_=1 then set sums;
  cum_Bg + Bg;
  Fx_Bg = 	sum_bg/2 - cum_Bg;
  Sign_Bg = sign(fx_bg);
  cum_He + He;
  Fx_He = sum_he/2 - cum_He;
  Sign_He = sign(fx_he);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So if Bg and He represent the variables you need to process then you might use macro code to generate those blocks that use them.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;... 
sum=sum_Bg sum_He  ;
...
  cum_Bg + Bg;
  Fx_Bg = 	sum_bg/2 - cum_Bg;
  Sign_Bg = sign(fx_bg);
  cum_He + He;
  Fx_He = sum_he/2 - cum_He;
  Sign_He = sign(fx_he);
...&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But you could also transpose the data first. Then no need to generate code (other than the list of variables in the VAR statement of the TRANSPOSE step).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=FINAL out=TALL(rename=(col1=VALUE)) name=VARNAME ;
  by Eink;
  var Bg He ;
run;

proc sort data=TALL;
  by varname eink;
run;

proc summary data=TALL nway;
  by varname ;
  var value;
  output out=sums(drop=_freq_ _type_) sum=sum_value ;
run;

data step4_tall;
  merge tall sums ;
  by varname ;
  if _n_=1 then do;
    set min_max;
    declare hash h(dataset:'min_max');
    h.definekey('eink');
    h.definedata('Xmin','Xmax');
    h.definedone();
  end;
  if first.varnum then cum_value=.;
  h.find();
  cum_value + value ;
  fx_value = sum_value/2 - cum_value ;
  Sign_value = sign(fx_value);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 16 Jun 2023 17:56:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/loop-for-changing-variables-in-changing-datasets/m-p/881151#M348177</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-06-16T17:56:35Z</dc:date>
    </item>
    <item>
      <title>Re: loop for changing variables in changing datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/loop-for-changing-variables-in-changing-datasets/m-p/881159#M348179</link>
      <description>&lt;P&gt;What is STEP5 to STEP11 doing?&lt;/P&gt;
&lt;P&gt;Why is it using RANK instead of just taking the MIN and MAX?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=step4_tall nway ;
  by varname sum_value ;
  class sign_value ;
  var cum_value fx_value ;
  output out=stats min= max= / autoname ;
run;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;                                                             cum_                   cum_
                    sum_     Sign_                         value_    fx_value_     value_    fx_value_
Obs    VARNAME     value     value    _TYPE_    _FREQ_        Min       Min         Max         Max

 1       Bg       3358.21      -1        1        18      2062.40     -1679.10    3358.21      -383.30
 2       Bg       3358.21       1        1         6        55.49        46.09    1633.02      1623.62
 3       He       2109.85      -1        1        15      1176.55     -1054.93    2109.85      -121.62
 4       He       2109.85       1        1         9         2.72        15.43    1039.50      1052.20
&lt;/PRE&gt;</description>
      <pubDate>Fri, 16 Jun 2023 17:58:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/loop-for-changing-variables-in-changing-datasets/m-p/881159#M348179</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-06-16T17:58:12Z</dc:date>
    </item>
    <item>
      <title>Re: loop for changing variables in changing datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/loop-for-changing-variables-in-changing-datasets/m-p/881161#M348181</link>
      <description>&lt;P&gt;At this point I think I am going call "Foul" on use of the word "median" at all to describe this process.&lt;/P&gt;
&lt;P&gt;When I use that example data and run the code the result I get is&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Obs     Eink                  Bg                 He

 1     Median    1526.8329804346    2278.1399438082


&lt;/PRE&gt;
&lt;P&gt;Every definition I have run into for median relates to "being in the middle" of values with some slop as to exactly how that middle is defined. Or perhaps "Formally, a median of a &lt;A class="mw-redirect" title="Population (statistics)" href="https://en.wikipedia.org/wiki/Population_(statistics)" target="_blank"&gt;population&lt;/A&gt; is any value such that at least half of the population is less than or equal to the proposed median and at least half is greater than or equal to the proposed median."&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since the largest value in your Final example data for Bg is 461.6179330 I see no way that a value of 1526 or greater should be considered any sort of "median" for Bg. Similar with your He variable as the max in the set is 192.283645.&lt;/P&gt;
&lt;P&gt;This may be a statistic of some sort but a median it ain't.&lt;/P&gt;</description>
      <pubDate>Fri, 16 Jun 2023 18:12:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/loop-for-changing-variables-in-changing-datasets/m-p/881161#M348181</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-06-16T18:12:58Z</dc:date>
    </item>
    <item>
      <title>Re: loop for changing variables in changing datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/loop-for-changing-variables-in-changing-datasets/m-p/881164#M348183</link>
      <description>&lt;P&gt;So that last formula appears to interpolating between XMIN and XMAX based on the something that looks like perhaps the range at the point where the cum sum crosses the 50% point of the total sum.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Bg =	&amp;amp;Xmin_B.  	+ &amp;amp;b_B.*((&amp;amp;n2_B.-&amp;amp;Fx_1_B.)/&amp;amp;fx_B.);
He 	=	&amp;amp;Xmin_H.  	+ &amp;amp;b_H.*((&amp;amp;n2_H.-&amp;amp;Fx_1_H.)/&amp;amp;fx_H.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Since you appear to have gotten it to run &lt;STRONG&gt;what are the values of those 5 macro variables for each analysis&lt;/STRONG&gt; variable that ended up being used to make your result?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since they appear from the convoluted steps 5 to 10 be just pulling values from the dataset we should be able to look at the numbers and figure out which values the formula is using (much easier than figuring out what that code is trying to pull from looking at it).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Jun 2023 18:31:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/loop-for-changing-variables-in-changing-datasets/m-p/881164#M348183</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-06-16T18:31:47Z</dc:date>
    </item>
    <item>
      <title>Re: loop for changing variables in changing datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/loop-for-changing-variables-in-changing-datasets/m-p/881165#M348184</link>
      <description>&lt;P&gt;For your example data what values did the macro variables used in that last step end up having?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Bg = &amp;amp;Xmin_B.   + &amp;amp;b_B.*((&amp;amp;n2_B.-&amp;amp;Fx_1_B.)/&amp;amp;fx_B.);
He  = &amp;amp;Xmin_H.   + &amp;amp;b_H.*((&amp;amp;n2_H.-&amp;amp;Fx_1_H.)/&amp;amp;fx_H.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you don't know re-run it with the SYMBOLGEN option turned on and the SAS log will show you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Are they basically just the values from these rows where the value on the FX variable flips from positive to negative?&lt;/P&gt;
&lt;PRE&gt;                                       sum_                       cum_                 Sign_
Obs    Eink    VARNAME     VALUE      value     Xmin    Xmax     value     fx_value    value

  6     06       Bg       461.618    3358.21    1250    1500    1633.02      46.087       1
  7     07       Bg       429.382    3358.21    1500    1750    2062.40    -383.295      -1

 33     09       He       180.849    2109.85    2000    2250    1039.50      15.426       1
 34     10       He       137.045    2109.85    2250    2500    1176.55    -121.620      -1

&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Jun 2023 18:43:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/loop-for-changing-variables-in-changing-datasets/m-p/881165#M348184</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-06-16T18:43:16Z</dc:date>
    </item>
    <item>
      <title>Re: loop for changing variables in changing datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/loop-for-changing-variables-in-changing-datasets/m-p/881221#M348186</link>
      <description>&lt;P&gt;This gets you really close. You'll need to play around with the rounding to get an exact match.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data final;
    input Eink :$2. Bg He;
    cards;
01 55.487739 2.722371
02 127.485645 18.670678
03 182.95309 53.529988
04 354.69612 122.703093
05 450.777081 137.596942
06 461.617933 157.457164
07 429.38197 192.283645
08 370.644235 173.688658
09 281.256479 180.848677
10 170.426973 137.045263
11 118.22022 128.45571
12 93.656461 104.596442
13 68.561622 112.710123
14 42.443895 77.742469
15 50.933162 136.718895
16 35.285987 104.219131
17 23.123863 78.424041
18 16.724659 89.48717
19 9.271308 46.396113
20 4.870804 21.574738
21 4.83902 19.753305
22 3.487616 9.23105
23 0.860551 2.136266
24 1.202043 1.861731
;

data max_min;
    length Eink $2 Xmin Xmax 8;

    do xmax=250 to 3500 by 250, 4000, 4500, 5000, 6000, 7000, 8000, 10000, 15000, 
            25000, .;
        i+1;
        eink=put(i, z2.);
        xmin=sum(0, lag(xmax));
        output;
    end;
    drop i;
run;

data eink_2;
    merge final max_min;
    by eink;
run;

proc sort data=eink_2;
    by eink xmax xmin;
run;

proc sql noprint;
    select name into :var_list separated by " " from sashelp.vcolumn where 
        libname='WORK' and memname='EINK_2' and upcase(name) not in ('EINK', 'XMAX', 
        'XMIN');
quit;

%put &amp;amp;var_list.;

proc transpose data=eink_2 out=tall(rename=(col1=VALUE)) name=VARNAME;
    by Eink xmax xmin;
    var &amp;amp;var_list;
run;

proc sort data=tall;
    by varname eink;
run;

*calculate cumulative sum;

data eink_3;
    set tall;
    by varname;

    if first.varname then
        cum_sum=value;
    else
        cum_sum+value;
run;

*get totals and substract from cumulative numbers;

proc sql;
    create table eink_4 as select varname, value, xmin, xmax, cum_sum, 
        sum(value)/2 as sum_var, calculated sum_var - cum_sum as fx_pra from eink_3 
        group by varname order by varname, eink;
quit;

*rank data ( really just reverses eink order for everything greater than 0?);

proc rank data=eink_4 out=ranked_data;
    where fx_pra&amp;gt;0;
    by varname;
    var fx_pra;
    ranks rang;
run;

*get first ranks only;

data first_ranks;
    set ranked_data;
    where rang=1;
    cum_sum=round(cum_sum, 0.0000001);
    keep varname value sum_var cum_sum;
run;

*repeat rank process for &amp;gt;0;
*rank data ( really just reverses eink order for everything greater than 0?);

proc rank data=eink_4 descending out=ranked_data_reversed;
    where fx_pra&amp;lt;0;
    by varname;
    var fx_pra;
    ranks rang;
run;

*get first ranks only;

data first_ranks_reversed;
    set ranked_data_reversed;
    where rang=1;
    B=xmax-xmin;
    value=round(value, 0.0000001);
    keep varname xmin B Value;
    rename xmin=reversed_xmin value=reversed_value;
run;

data results;
    merge first_ranks first_ranks_reversed;
    by varname;
    MEDIAN=reversed_xmin + B*((sum_var-cum_sum)/reversed_value);
    format MEDIAN 15.12;
run;

proc transpose data=results out=want NAME=EINK;
    id varname;
    var MEDIAN;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 16 Jun 2023 19:47:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/loop-for-changing-variables-in-changing-datasets/m-p/881221#M348186</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-06-16T19:47:36Z</dc:date>
    </item>
    <item>
      <title>Re: loop for changing variables in changing datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/loop-for-changing-variables-in-changing-datasets/m-p/881230#M348189</link>
      <description>&lt;P&gt;Thanks, &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;.&amp;nbsp; So you just need to find the point where the cumulative sum crosses the 50% threshold.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/388612"&gt;@Konkordanz&lt;/a&gt;&amp;nbsp;I don't think you need PROC RANK and that other stuff.&amp;nbsp; And if you have explained what calculation you wanted:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;* The MEDIAN is generated by interpolating between XMIN and XMAX ;
* Amount is based on how much of current value is before 50 percent ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;at the start we could have found a simple solution much faster.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is a macro that just needs the input dataset and the variable list and does it all in one data step. So you can run it for as many datasets and variables as you have.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(Note: The XMIN/XMAX values need to already be in the input dataset).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro adj_median
(indsn    /* Input dataset */
,varlist  /* space delimited variable list */
,outdsn=medians /* Output dataset */
);
%local n ;
%* Count how many variables there are ;
%let n=%sysfunc(countw(&amp;amp;varlist,%str( ),q));
data &amp;amp;outdsn ;
* Setup arrays ;
  array vars &amp;amp;varlist ;
  array total total1-total&amp;amp;n ;
  array subtot subtot1-subtot&amp;amp;n ;
  array median median1-median&amp;amp;n ;
* Calculate Totals ;
  do until(eof1);
    set &amp;amp;indsn end=eof1;
    do over vars;
      total+vars;
    end;
  end;
* Find 50 percent of totals ;
  do over total;
    total=total/2;
  end;
* Calculate cummulative total until cross half of total ;
* Continue until either hit the end of the data or have ;
* found median for every variable;
  do until(eof2 or n&amp;gt;=&amp;amp;n);
    set &amp;amp;indsn end=eof2;
    do over vars;
      subtot+vars;
      if subtot &amp;gt; total and missing(median) then do;
* The MEDIAN is generated by interpolating between XMIN and XMAX ;
* Amount is based on how much of current value is before 50 percent ;
        median = xmin + (xmax-xmin)*((total+vars-subtot)/vars) ;
        n+1;
      end;
    end;
  end;
* Store the medians back into the original variables;
  do over vars;
    vars = median;
  end;
* Only keep the original variables ;
  keep &amp;amp;varlist;
run;
%mend adj_median;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;PS I don't like typing [i] over and over and over and over so I just made it use old fashion implicit array references.&lt;/P&gt;
&lt;P&gt;So let's try if for our FINAL and MIN_MAX datasets.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data for_analysis;
  merge final min_max;
  by eink;
run;

options mprint;
%adj_median(indsn=for_analysis,varlist=Bg He,outdsn=WANT);

proc print data=want;
run;


&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt;Obs       Bg         He

 1     1526.83    2278.14

&lt;/PRE&gt;
&lt;P&gt;Log&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;PRE&gt;3648  options mprint;
3649  %adj_median(indsn=for_analysis,varlist=Bg He,outdsn=WANT);
MPRINT(ADJ_MEDIAN):   data WANT ;
MPRINT(ADJ_MEDIAN):   * Setup arrays ;
MPRINT(ADJ_MEDIAN):   array vars Bg He ;
MPRINT(ADJ_MEDIAN):   array total total1-total2 ;
MPRINT(ADJ_MEDIAN):   array subtot subtot1-subtot2 ;
MPRINT(ADJ_MEDIAN):   array median median1-median2 ;
MPRINT(ADJ_MEDIAN):   * Calculate Totals ;
MPRINT(ADJ_MEDIAN):   do until(eof1);
MPRINT(ADJ_MEDIAN):   set for_analysis end=eof1;
MPRINT(ADJ_MEDIAN):   do over vars;
MPRINT(ADJ_MEDIAN):   total+vars;
MPRINT(ADJ_MEDIAN):   end;
MPRINT(ADJ_MEDIAN):   end;
MPRINT(ADJ_MEDIAN):   * Find 50 percent of totals ;
MPRINT(ADJ_MEDIAN):   do over total;
MPRINT(ADJ_MEDIAN):   total=total/2;
MPRINT(ADJ_MEDIAN):   end;
MPRINT(ADJ_MEDIAN):   * Calculate cummulative total until cross half of total ;
MPRINT(ADJ_MEDIAN):   * Continue until either hit the end of the data or have ;
MPRINT(ADJ_MEDIAN):   * found median for every variable;
MPRINT(ADJ_MEDIAN):   do until(eof2 or n&amp;gt;=2);
MPRINT(ADJ_MEDIAN):   set for_analysis end=eof2;
MPRINT(ADJ_MEDIAN):   do over vars;
MPRINT(ADJ_MEDIAN):   subtot+vars;
MPRINT(ADJ_MEDIAN):   if subtot &amp;gt; total and missing(median) then do;
MPRINT(ADJ_MEDIAN):   * The MEDIAN is generated by interpolating between XMIN and XMAX ;
MPRINT(ADJ_MEDIAN):   * Amount is based on how much of current value is before 50 percent ;
MPRINT(ADJ_MEDIAN):   median = xmin + (xmax-xmin)*((total+vars-subtot)/vars) ;
MPRINT(ADJ_MEDIAN):   n+1;
MPRINT(ADJ_MEDIAN):   end;
MPRINT(ADJ_MEDIAN):   end;
MPRINT(ADJ_MEDIAN):   end;
MPRINT(ADJ_MEDIAN):   * Store the medians back into the original variables;
MPRINT(ADJ_MEDIAN):   do over vars;
MPRINT(ADJ_MEDIAN):   vars = median;
MPRINT(ADJ_MEDIAN):   end;
MPRINT(ADJ_MEDIAN):   * Only keep the original variables ;
MPRINT(ADJ_MEDIAN):   keep Bg He;
MPRINT(ADJ_MEDIAN):   run;

NOTE: There were 24 observations read from the data set WORK.FOR_ANALYSIS.
NOTE: There were 11 observations read from the data set WORK.FOR_ANALYSIS.
NOTE: The data set WORK.WANT has 1 observations and 2 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
&lt;/PRE&gt;
&lt;/LI-SPOILER&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 18 Jun 2023 15:55:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/loop-for-changing-variables-in-changing-datasets/m-p/881230#M348189</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-06-18T15:55:59Z</dc:date>
    </item>
    <item>
      <title>Re: loop for changing variables in changing datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/loop-for-changing-variables-in-changing-datasets/m-p/881541#M348327</link>
      <description>&lt;P&gt;Oh, thank you for your effort and your responses! I will look at them in the coming days and get back to you. This will take a while because I need to get into your detailed answers and the many syntax steps that are new to me. Thanks!&lt;/P&gt;</description>
      <pubDate>Tue, 20 Jun 2023 13:49:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/loop-for-changing-variables-in-changing-datasets/m-p/881541#M348327</guid>
      <dc:creator>Konkordanz</dc:creator>
      <dc:date>2023-06-20T13:49:22Z</dc:date>
    </item>
  </channel>
</rss>

