SAS macro and Proc univariate: create histogram for each group with different endpoints

Accepted Solution Solved
Reply
Contributor
Posts: 33
Accepted Solution

SAS macro and Proc univariate: create histogram for each group with different endpoints

Hi, 

 

I have 2 variables in my dataset - y and group. For each group, I want to create a histogram with endpoints calculated within the data for each group. My data could change, therefore,  the number of observations and number of groups may change for every diffrent data. For each group, i calculated the bandwidth(bw), lower limit (lw) and upper limit (up). I have to use these values to specify the endpoints for each histogram. But I keep on getting errors. Do you have any suggestion to improve and correct my current code? I want to output only the histogram for each group with specific endpoints. Thank you. 

 

data test;
input y group @@;
datalines;
35 1 46 2
32 1 43 2
38 1 46 2
32 1 46 2
32 1 40 2
38 1 50 2
36 1 49 2
33 1 40 3
37 1 42 3
33 1 40 3
49 2 42 3
45 2 43 3
49 2 45 3
41 2 45 3
47 2 49 3
48 2 40 3
45 2 58 3
47 3 59 3
56 4 56 3
52 4 51 4
60 4 57 4
60 4 53 4
50 4 50 4
56 4 52 4
53 4 51 4
50 4 52 4
;
run;

 

%macro histo;
proc means data=test min max range;
by group;
var y;
output out=dishist2(drop=_TYPE_ _FREQ_) min=min max=max range=range;
run;

 

data dishist2;
set dishist2;
bw=(max-min)/10;
lw=floor(min/bw)*bw-(3*bw);
up=floor(((max/bw)+1)*bw)+(3*bw);
run;

 

proc sql noprint ;
select bw ,lw, up
into :bw1 separated by ' ',
:lw1 separated by ' ',
:up1 separated by ' '
from dishist2;
quit;
%let cnt=&sqlobs;

%do i=1 %to &cnt;
%let lww= %scan(&lw1,&i);
%let upp= %scan(&up1,&i);
%let bww= %scan(&bw1,&i);


proc univariate data=test noprint ;
by set;
histogram resid/ grid normal name='MyHist' vscale=proportion hoffset=10 vaxis=0 to 1 by .5
endpoints = &lww to &upp by &bww;
run;
%end;
%mend histo;

%histo;


Accepted Solutions
Solution
‎11-09-2015 03:55 PM
Respected Advisor
Posts: 4,804

Re: SAS macro and Proc univariate: create histogram for each group with different endpoints

There were many errors in your code. A corrected version is:

 

data test;
input y group;
datalines;
35 1 46 2
32 1 43 2
38 1 46 2
32 1 46 2
32 1 40 2
38 1 50 2
36 1 49 2
33 1 40 3
37 1 42 3
33 1 40 3
49 2 42 3
45 2 43 3
49 2 45 3
41 2 45 3
47 2 49 3
48 2 40 3
45 2 58 3
47 3 59 3
56 4 56 3
52 4 51 4
60 4 57 4
60 4 53 4
50 4 50 4
56 4 52 4
53 4 51 4
50 4 52 4
;
 
options mprint;
%macro histo;
proc means data=test min max range;
by group;
var y;
output out=dishist2(drop=_TYPE_ _FREQ_) min=min max=max range=range;
run;
 
data dishist2;
set dishist2;
where range > 0;
bw=range/10;
lw=floor(min/bw)*bw-3*bw;
up=ceil(max/bw)*bw+3*bw;
run;
 
proc sql;
select group, bw ,lw, up
into 
    :gr1 separated by '*',
    :bw1 separated by '*',
    :lw1 separated by '*',
    :up1 separated by '*'
from dishist2;
quit;

%let cnt=&sqlobs;
%do i=1 %to &cnt;
%let grp= %scan(&gr1,&i,*);
%let lww= %scan(&lw1,&i,*);
%let upp= %scan(&up1,&i,*);
%let bww= %scan(&bw1,&i,*);

proc univariate data=test noprint ;
where group=&grp;
histogram y/ grid normal name='MyHist' vscale=proportion hoffset=10 vaxis=0 to 1 by .5
endpoints = &lww to &upp by &bww;
run;
%end;
%mend histo;
%histo;
PG

View solution in original post


All Replies
Trusted Advisor
Posts: 1,783

Re: SAS macro and Proc univariate: create histogram for each group with different endpoints


statz wrote:
But I keep on getting errors.


Do tell! What errors?

Contributor
Posts: 33

Re: SAS macro and Proc univariate: create histogram for each group with different endpoints

I get these errors: 

ERROR: An increment of zero is not allowed in a list of the form "a to b by 0".

NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE UNIVARIATE used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

 


ERROR: At least two endpoints must be specified.
NOTE: PROCEDURE UNIVARIATE used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

 

Thanks.

Trusted Advisor
Posts: 1,783

Re: SAS macro and Proc univariate: create histogram for each group with different endpoints

Your PROC MEANS generates an error, that the data is not sorted properly.

 

What happens if you fix that and then re-run the entire code?

Contributor
Posts: 33

Re: SAS macro and Proc univariate: create histogram for each group with different endpoints

Here is the updated code: 

 

data test;
input y group @@;
datalines;
35 1 46 2
32 1 43 2
38 1 46 2
32 1 46 2
32 1 40 2
38 1 50 2
36 1 49 2
33 1 40 3
37 1 42 3
33 1 40 3
49 2 42 3
45 2 43 3
49 2 45 3
41 2 45 3
47 2 49 3
48 2 40 3
45 2 58 3
47 3 59 3
56 4 56 3
52 4 51 4
60 4 57 4
60 4 53 4
50 4 50 4
56 4 52 4
53 4 51 4
50 4 52 4
;
run;

%macro histo;
proc sort data=test out=test1;
by group;
run;

proc means data=test1 min max range;
by group;
var y;
output out=dishist2(drop=_TYPE_ _FREQ_) min=min max=max range=range;
run;

data dishist2;
set dishist2;
bw=(max-min)/10;
lw=floor(min/bw)*bw-(3*bw);
up=floor(((max/bw)+1)*bw)+(3*bw);
run;

proc sql noprint ;
select bw ,lw, up
into :bw1 separated by ' ',
:lw1 separated by ' ',
:up1 separated by ' '
from dishist2;
quit;
%let cnt=&sqlobs;

%do i=1 %to &cnt;
%let lww= %scan(&lw1,&i);
%let upp= %scan(&up1,&i);
%let bww= %scan(&bw1,&i);


proc univariate data=test1 noprint ;
by group;
histogram y/ grid normal name='MyHist' vscale=proportion hoffset=10 vaxis=0 to 1 by .5
endpoints = &lww to &upp by &bww;
run;
%end;
%mend histo;

%histo;

 

 

Here is the error that I get; 

NOTE: PROCEDURE SQL used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds


ERROR: An increment of zero is not allowed in a list of the form "a to b by 0".

NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE UNIVARIATE used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

 


ERROR: At least two endpoints must be specified.
NOTE: PROCEDURE UNIVARIATE used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

 

WARNING: The ENDPOINTS= list was extended to accommodate the data.
NOTE: The above message was for the following BY group:
group=1
WARNING: The ENDPOINTS= list was extended to accommodate the data.
NOTE: The above message was for the following BY group:
group=3
WARNING: The ENDPOINTS= list was extended to accommodate the data.
NOTE: The above message was for the following BY group:
group=4
NOTE: PROCEDURE UNIVARIATE used (Total process time):
real time 1.89 seconds
cpu time 0.43 seconds

 

NOTE: PROCEDURE UNIVARIATE used (Total process time):
real time 1.03 seconds
cpu time 0.35 seconds

 

 

Trusted Advisor
Posts: 1,783

Re: SAS macro and Proc univariate: create histogram for each group with different endpoints

You need to specify that the delimiter used by all of your calls to %SCAN is a blank, for example:

 

%let bww= %scan(&bw1,&i,%str( ));
Solution
‎11-09-2015 03:55 PM
Respected Advisor
Posts: 4,804

Re: SAS macro and Proc univariate: create histogram for each group with different endpoints

There were many errors in your code. A corrected version is:

 

data test;
input y group;
datalines;
35 1 46 2
32 1 43 2
38 1 46 2
32 1 46 2
32 1 40 2
38 1 50 2
36 1 49 2
33 1 40 3
37 1 42 3
33 1 40 3
49 2 42 3
45 2 43 3
49 2 45 3
41 2 45 3
47 2 49 3
48 2 40 3
45 2 58 3
47 3 59 3
56 4 56 3
52 4 51 4
60 4 57 4
60 4 53 4
50 4 50 4
56 4 52 4
53 4 51 4
50 4 52 4
;
 
options mprint;
%macro histo;
proc means data=test min max range;
by group;
var y;
output out=dishist2(drop=_TYPE_ _FREQ_) min=min max=max range=range;
run;
 
data dishist2;
set dishist2;
where range > 0;
bw=range/10;
lw=floor(min/bw)*bw-3*bw;
up=ceil(max/bw)*bw+3*bw;
run;
 
proc sql;
select group, bw ,lw, up
into 
    :gr1 separated by '*',
    :bw1 separated by '*',
    :lw1 separated by '*',
    :up1 separated by '*'
from dishist2;
quit;

%let cnt=&sqlobs;
%do i=1 %to &cnt;
%let grp= %scan(&gr1,&i,*);
%let lww= %scan(&lw1,&i,*);
%let upp= %scan(&up1,&i,*);
%let bww= %scan(&bw1,&i,*);

proc univariate data=test noprint ;
where group=&grp;
histogram y/ grid normal name='MyHist' vscale=proportion hoffset=10 vaxis=0 to 1 by .5
endpoints = &lww to &upp by &bww;
run;
%end;
%mend histo;
%histo;
PG
Contributor
Posts: 33

Re: SAS macro and Proc univariate: create histogram for each group with different endpoints

Thanks PG! Appreciate your help!
🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 604 views
  • 0 likes
  • 3 in conversation