I have the following Dataset as below with the followin variables Subject (character),
Value1,value2,value3,value4 (numeric)
Subject Value1 value2 value3 value4
------- ------ ------ ------ ------
10001 3 4 5 8
10007 4 6 4 9
10009 4 9 10 3
I need a folwwoing dataset as below which contains the maximum values for
Value1,value2,value3,value4.
The result dataset should be as below:
Subject Value
------- -----
10001 8
10007 9
10009 10
Thanks a lot.
data have;
input Subject $ Value1 value2 value3 value4;
value=max(of value:);
datalines;
10001 3 4 5 8
10007 4 6 4 9
10009 4 9 10 3
;
Sorry there is no variable patern. it can have any name or any number od varaibles. can you still help me? might need to write a macro.
So how do you decide which variables to do the mean of? Is it all numerics?
data have;
array allnumbers (*) _numeric_;
input Subject $ Value1 value2 value3 value4;
value=max(of allnumbers);
datalines;
10001 3 4 5 8
10007 4 6 4 9
10009 4 9 10 3
;
yes they are numeric and also it is good if I have a choice to pick up the varaibles as a macro parameter. thanks a lot
You would need to move the ARRAY statement to follow the INPUT statement for this to work. But you could probably get by without an ARRAY:
value = max(of _numeric_);
There are other ways to form variable lists in SAS. For example, get the max of all variable names that begin with "value":
value = max(of value:);
But the problem needs a little more clarity on what is actually required.
Can I have a choice to pick up the varaibles as a macro parameter. such as I want var1 and varible 3 only. can you please show how to do it using macro?
Thanks
%let mean_var_list = value1 value3;
data have;
array numbers (*) &mean_var_list;
input Subject $ Value1 value2 value3 value4;
value=max(of numbers);
datalines;
10001 3 4 5 8
10007 4 6 4 9
10009 4 9 10 3
;
run;
Thanks. And If I have multiple dataset to work with. I have almost 60 datasets. can I pass the dataset name as a parameter? Can you please show me how to do it?
When you have problems like this its best to illustrate the whole problem. It's also good to try and write something yourself to show you're at trying rather than just asking others to write code. Another option is to look for pre-written macros that are similar to what you need on here, lexjansen.com or google searches.
Here's a good macro tutorial for beginners:
Statistical Computing Seminar: Introduction to SAS Macro Language
Start by defining the entire problem, what you have and where you want to go. A good way to write a macro is to start with the definition, what parameters you'll need to pass in
%macro summarize(dataset_name=, variable_list=, output_data=);
*rest of SAS Code;
%mend summarize;
Hi Reeza,
Thanks for your help. I am new to macro. I ran the following code and the got the following error regarding array. Can you please lot at the below and let me know? Thanks
%macro summarize(dataset_name=, variable_list=, output_data=);
data have&output_data;
1set &RAWLIB..&dataset_name;
array numbers (*) &variable_list;
value=max(of numbers);
%mend summarize;
%summarize(dataset_name=A01MAIN, variable_list= STDENDT EDRUGENDT, output_data=k);
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.HAVEK may be incomplete. When this step was stopped there were 0
observations and 62 variables.
WARNING: Data set WORK.HAVEK was not replaced because this step was stopped.
NOTE: DATA statement used (Total process time):
real time 3:14.56
cpu time 4.14 seconds
NOTE: Data file MBC301.A01MAIN.DATA is in a format that is native to another host, or the file
encoding does not match the session encoding. Cross Environment Data Access will be used,
which might require additional CPU resources and might reduce performance.
ERROR: Illegal reference to the array numbers. ---------> ERROR
Your max of statement is incorrect. Does the following work?
%macro summarize(dataset_name=, variable_list=, output_data=);
data have&output_data;
1set &RAWLIB..&dataset_name;
array numbers (*) &variable_list;
value=max(of numbers(*));
%mend summarize;
%summarize(dataset_name=A01MAIN, variable_list= STDENDT EDRUGENDT, output_data=k);
You are not only smart but also very helpful. You saved me a few times. Thanks a lot
data need;
set have;
value = max(of value1 - value4);
run;
As long as all of the variables are adjacent in the pdf, you can use a variable list:
data have;
input Subject $ Value1 value2 value3 value4;
value=max(of value1--value4);
datalines;
10001 3 4 5 8
10007 4 6 4 9
10009 4 9 10 3
;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.