BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
hellohere
Pyrite | Level 9

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;
1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

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;

Patrick_0-1701086893814.png

 

 

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

More explanation needed. Here is a view of the data from your program

 

PaigeMiller_0-1701086455607.png

 

 

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?

 

--
Paige Miller
Patrick
Opal | Level 21

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;

Patrick_0-1701086893814.png

 

 

hellohere
Pyrite | Level 9
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.
hellohere
Pyrite | Level 9

In two consecutive BLOCs. So there are 19 ranges.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 540 views
  • 1 like
  • 3 in conversation