简体   繁体   中英

How do I calculate range of a variable in SAS?

I have a table in SAS which has a variable say X. I want to know only the range of X, I used PROC UNIVARIATE, but it gives out a lot of other information. I have been trying to use RANGE function in the following way, but doesn't yield any result. Please help!

DATA DATASET2;
SET DATASET1;
R=RANGE(X);
KEEP R;
RUN;
PROC PRINT DATASET2;

RUN;

the range function is for within a row and you have tried for column, so probably you might have got zeros. range function can be used as follows.

  R= range(x,y,x);

For within an column you need use proc means.

 proc means data=sashelp.class range maxdec=2;
  var age;
  run;

or by using proc sql as shown below.

 proc sql;
select max(age) -min(age) as range
from sashelp.class;

You can also use the range function in proc sql, where it operates on columns rather than rows:

proc sql;
  select range(age) from sashelp.class;
quit;

This is also possible within a data step, if you don't like sql:

data _null_;
  set sashelp.class end = eof;
  retain min_age max_age;
  min_age = min(age,min_age);
  max_age = max(age,max_age);
  if eof then do;
    range = max_age - min_age;
    put range= min_age= max_age=;
  end;
run;

Or equivalently:

data _null_;
  do until (eof);
    set sashelp.class end = eof;
    min_age = min(age,min_age);
    max_age = max(age,max_age);
  end;
  range = max_age - min_age;
  put range= min_age= max_age=;
run;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM