%macro intfmt( /* This macro generates SAS Proc Format value code for breaking down an interval of numeric values. For example if lims argument is "0 15 20 50 80 high" the generated code is 0-14="0 to 14" 15-19="15 to 19" 20-49="20-49" 50-79="50-79" 80-high="80 and over" Same value for lims but with optional parm integers=0 would result in 0-15="0 to 15" 15<-19="15 to 20" 20<-50="20-49" 50<-80="50-79" 80-high="Over 80" Note that generated code is only the interval specs, not the entire Value statement. So it might be just a portion of a value statement. J. Blodgett, OSEDA, Dec. 2001 */ lims, /* list of interval limits. Should be an ascending list of numbers. First entry can be the special value "low" and the last entry can be "high". */ integers=1, /* if integers=1 (true) then the upper limit of each interval is 1 less than the lower limit of the next interval. See example, above. Specifying integers=0 causes the macro to generate continuous intervals with each interval after the first open on the lower end and closed on the upper end. */ debug=0); %local start end next next2 i arg label nextend lasti; %let lims=%lowcase(&lims); %*--keywords LOW and HIGH will be converted to low and high-; %let i=1; %let next= %scan(&lims,2) ; %if &next = %str() %then %do; %put ****Invalid lims parms passed to intfmt macro: Must have at least 2 values****; %goto endmac; %end; %do %while (&next ne ); %let start= %scan(&lims,&i); %*--If we are dealing with integers and it is not the last interval then we subtract 1 from the upper limit. E.g. if lims is 10 20 30 then we want 10-19 and 20-30.-; %if &integers and &next ne high and %scan(&lims,%eval(&i+2)) ne %str() %then %let end=%eval(&next - 1); %else %let end=&next; %*--define arg, the left side of the value entry--; %if &integers or &i=1 %then %let arg= &start - &end; %else %let arg= &start <- &end; %*--define label, the right side of the value entry--; %if &i=1 and &start=low %then %if &integers %then %let label=Under &next; %else %let label=&next and under; %else %if &next=high %then %if &integers %then %let label=&start and over; %else %let label=Over &start; %else %let label= &start to &end; %if &integers and &start eq &end %then %let label=&start; %*--generate the value interval (macro output) right here-------------------------; &arg = "&label" %let i=%eval(&i+1); %let next=%scan(&lims, %eval(&i+1)); %end; %*--do while loop---; %endmac: %mend intfmt; /* Test code, commented out. options mprint NOsymbolgen NOmlogic; proc format; value age %intfmt(low 1 2 3 4 5 15 50 ,integers=1) other='Other'; run; data ages; do age=1 to 50; output; end; run; proc freq data=ages; table age; format age age.; run; */