Each row contains Max or Min (intervened 1-by-1). How to get the range , in data step ?!
Sample code, with dataset, is below.
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_>=0 then do;
if &selvar._min<-0.0001 then do;
&selvar._range= lag(&selvar._max)- &selvar._min;
t_max=lag(&selvar._max);
end;
else if &selvar._max> 0.0001 then do;
&selvar._range= &selvar._max - lag(&selvar._min);
t_min=lag(&selvar._min);
end;
end;
run;quit;
Thanks for providing sample data and code. It unfortunately still didn't fully explain to me what you have and what you want.
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.
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;
More explanation needed. Here is a view of the data from your program
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?
Or do you want the var_max of the entire data set minus the var_min of the entire data set?
Thanks for providing sample data and code. It unfortunately still didn't fully explain to me what you have and what you want.
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.
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;
In two consecutive BLOCs. So there are 19 ranges.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.