<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to get the range (max-min) from intervened rows, in data step?! in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-range-max-min-from-intervened-rows-in-data-step/m-p/904621#M357392</link>
    <description>&lt;P&gt;Thanks for providing sample data and code. It unfortunately still didn't fully explain to me what you have and what you want.&lt;/P&gt;
&lt;P&gt;By making a lot of assumptions like that your first data step creates HAVE data that you can't change and that this range is just the diff between your max and min values spread over two consecutive rows, below some sample code.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have(rename=(i=bloc));
  do i=1 to 20;
    if mod(i,2)=1 then
      do;
        var_min=-ranuni(i)*100;
        var_max=0;
      end;
    else
      do;
        var_max=ranuni(i)*100;
        var_min=0;
      end;
    output;
  end;
run;

data want;
  do i=1 to nobs;
    set have(keep=var_min) nobs=nobs point=i;
    i+1;
    set have(keep=var_max) point=i;
    var_range= var_max - var_min;
    output;
  end;
  stop;
run;

proc print data=want;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1701086893814.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/90211iDEF9EA16B93FDE00/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1701086893814.png" alt="Patrick_0-1701086893814.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>Mon, 27 Nov 2023 12:28:30 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2023-11-27T12:28:30Z</dc:date>
    <item>
      <title>How to get the range (max-min) from intervened rows, in data step?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-range-max-min-from-intervened-rows-in-data-step/m-p/904615#M357390</link>
      <description>&lt;P&gt;Each row contains Max or Min (intervened 1-by-1). How to get the range , in data step ?!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sample code, with dataset, is below.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _temp(rename=(i=bloc));
do i=1 to 20;
	if mod(i,2)=1 then do;
		var_min=-ranuni(i)*100;
		var_max=0;
	end;
	else do;
		var_max=ranuni(i)*100;
		var_min=0;
	end;
	output;
end;
run;quit;

%let selvar=var;
data _temp; set _temp; 
if _N_&amp;gt;=0 then do;
	if &amp;amp;selvar._min&amp;lt;-0.0001 then do;
		&amp;amp;selvar._range= lag(&amp;amp;selvar._max)- &amp;amp;selvar._min;
		t_max=lag(&amp;amp;selvar._max);
	end;
	else if &amp;amp;selvar._max&amp;gt; 0.0001 then do;
		&amp;amp;selvar._range= &amp;amp;selvar._max - lag(&amp;amp;selvar._min);
		t_min=lag(&amp;amp;selvar._min);
	end;
end;
run;quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 27 Nov 2023 11:29:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-range-max-min-from-intervened-rows-in-data-step/m-p/904615#M357390</guid>
      <dc:creator>hellohere</dc:creator>
      <dc:date>2023-11-27T11:29:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the range (max-min) from intervened rows, in data step?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-range-max-min-from-intervened-rows-in-data-step/m-p/904619#M357391</link>
      <description>&lt;P&gt;More explanation needed. Here is a view of the data from your program&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="PaigeMiller_0-1701086455607.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/90210i1F3839E7926F36E5/image-size/medium?v=v2&amp;amp;px=400" role="button" title="PaigeMiller_0-1701086455607.png" alt="PaigeMiller_0-1701086455607.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you want (row 2 var_max - row 1 var_min) on a row followed by (row 4 var_max - row 3 var_min) on another row?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or do you want the var_max of the entire data set minus the var_min of the entire data set?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 27 Nov 2023 12:03:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-range-max-min-from-intervened-rows-in-data-step/m-p/904619#M357391</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-11-27T12:03:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the range (max-min) from intervened rows, in data step?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-range-max-min-from-intervened-rows-in-data-step/m-p/904621#M357392</link>
      <description>&lt;P&gt;Thanks for providing sample data and code. It unfortunately still didn't fully explain to me what you have and what you want.&lt;/P&gt;
&lt;P&gt;By making a lot of assumptions like that your first data step creates HAVE data that you can't change and that this range is just the diff between your max and min values spread over two consecutive rows, below some sample code.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have(rename=(i=bloc));
  do i=1 to 20;
    if mod(i,2)=1 then
      do;
        var_min=-ranuni(i)*100;
        var_max=0;
      end;
    else
      do;
        var_max=ranuni(i)*100;
        var_min=0;
      end;
    output;
  end;
run;

data want;
  do i=1 to nobs;
    set have(keep=var_min) nobs=nobs point=i;
    i+1;
    set have(keep=var_max) point=i;
    var_range= var_max - var_min;
    output;
  end;
  stop;
run;

proc print data=want;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1701086893814.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/90211iDEF9EA16B93FDE00/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1701086893814.png" alt="Patrick_0-1701086893814.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>Mon, 27 Nov 2023 12:28:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-range-max-min-from-intervened-rows-in-data-step/m-p/904621#M357392</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-11-27T12:28:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the range (max-min) from intervened rows, in data step?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-range-max-min-from-intervened-rows-in-data-step/m-p/904623#M357393</link>
      <description>Thanks. It solves 50%. 

Take the two columns (_max/_min) as Peak and Trough. Range is the diff between the peak and trough in consecutive BLOC.</description>
      <pubDate>Mon, 27 Nov 2023 12:59:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-range-max-min-from-intervened-rows-in-data-step/m-p/904623#M357393</guid>
      <dc:creator>hellohere</dc:creator>
      <dc:date>2023-11-27T12:59:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the range (max-min) from intervened rows, in data step?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-range-max-min-from-intervened-rows-in-data-step/m-p/904624#M357394</link>
      <description>&lt;P&gt;In two consecutive BLOCs. So there are 19 ranges.&lt;/P&gt;</description>
      <pubDate>Mon, 27 Nov 2023 13:01:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-range-max-min-from-intervened-rows-in-data-step/m-p/904624#M357394</guid>
      <dc:creator>hellohere</dc:creator>
      <dc:date>2023-11-27T13:01:20Z</dc:date>
    </item>
  </channel>
</rss>

