Hi ,
I need to know the minimum and maximum values of a numeric variable(var1) using data steps. I need the minimum and maximum values to recall them in the proc univariate code I am using to create a histogram. I need those values to specify the range for the x axis. I need to create a generic code for the minimum and maximum values as the histogram will be created for a different datasets every time. I am looking to create a macro of the proc univariate code.
Here is an example of the code I am working on:
proc univariate data =final;
var var1;
histogram / midpoints= 4 to 36 by 0.5;
run;
Now: instead of '4' , I need a variable name/code that translates into the minimum value of the variable and instead of '36' I need a variable name/code that translates into the maximum value of the variable.
i.e.
proc univariate data=final;
var var1;
histogram /midpoints= (code for minimum value) to (code for maximum value) by 0.5;
run;
Also, proc univariate does allow to output a dataset with the minimum and maximum values by using the syntax..
output out=strengthstats min=variable name of minimum value max=variable name of maximum values;
Any suggestion on how to pull these variables from the strengthstats dataset is also welcome.
Please let me know your ides and thoughts on it. Thanks.
You can use proc univariate to generate the output data set even with the no print option. The output dataset can then be used to create macro variables via call symputX. Or you can use proc sql directly. Please see the code below.
/*create datasets with min/max of variable*/
proc univariate data=sashelp.class noprint;
var weight;
output out=want1 min=weight_min max=weight_max;
run;
proc print data=want1;
run;
/*create macro variables using proc sql, check log for results*/
proc sql noprint;
select min(weight), max(weight) into :weight_min, :weight_max
from sashelp.class;
quit;
%put weight_min = &weight_min;
%put weight_max = &weight_max;
Also, proc univariate does allow to output a dataset with the minimum and maximum values by using the syntax..
output out=strengthstats min=variable name of minimum value max=variable name of maximum values;
Yes, proc univariate can produce a dataset with min/max via syntax.
Why do you need a data step?
I think you're venturing into macro variables and macro programming, have you looked into that at all?
Yes, I am familiar with macro variables and programming. As I mentioned , I am defining a macro which with contain the proc univariate code to develop histograms. The input data sets will be different and the minimum and maximum values for the variable var1 will be different each time for different datasets. Although , I am able to get summary stats with proc univariate, I have to output the min and max values to another dataset as I will be using the noprint option. I am looking to have a generic code that pulls in the minimum and maximum values in the midpoints option. I cannot put actual numbers in the midpoints option as the datasets are going to be different all the time. I hope I explained everything clearly. Do you have any suggestions for me ? I would appreciate your help.
You can use proc univariate to generate the output data set even with the no print option. The output dataset can then be used to create macro variables via call symputX. Or you can use proc sql directly. Please see the code below.
/*create datasets with min/max of variable*/
proc univariate data=sashelp.class noprint;
var weight;
output out=want1 min=weight_min max=weight_max;
run;
proc print data=want1;
run;
/*create macro variables using proc sql, check log for results*/
proc sql noprint;
select min(weight), max(weight) into :weight_min, :weight_max
from sashelp.class;
quit;
%put weight_min = &weight_min;
%put weight_max = &weight_max;
@Reeza. I did create the output data set with the minimum and maximum values in spite of the noprint option. But, I needed to pull those values in the midpoints option and the output dataset was not helping. I did try proc sql and created a table with the minimum and maximum values but didn't think further of assigning macros there. The macros using proc sql will work perfect in what I want to accomplish. Thanks so much !
Glad you got it working. Please mark your question(s) answered.
How do I do that ? I figured it the first time, but for now I have been searching for it. I couldn't do it for my last question as well.
Thanks. I will do it right away.
For some reason it doesn't work. I have asked SAS support for help.
Hi dr2014, I'm Anna Brown, community manager. I'm not sure why you're not seeing the button to mark correct or helpful answers. I can mark them for you. Which specific answer would you like to mark correct, and any as helpful?
Thanks for your contributions to the community!
Best,
Anna
Just fyi annabrown@sas.. the button for correct or helpful answers were not showing up when I used Internet Explorer, but does show up when I use Firefox . So I got to it finally. Thanks,
Oh good to hear you saw it at last, dr2014. I'm sorry about the browser issue - thank you for bringing it to our attention. We are looking into it with the communities platform.
Anna
Hi dr2014, by chance could you please advise what version of IE you are using?
It is 11. Thanks.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.