Help using Base SAS procedures

Getting the Max value

Reply
Contributor
Posts: 44

Getting the Max value

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.

Trusted Advisor
Posts: 1,228

Re: Getting the Max value

Posted in reply to need_sas_help

data have;

input Subject $ Value1    value2 value3  value4;

value=max(of valueSmiley Happy;

datalines;

10001     3        4       5       8

10007     4        6       4       9

10009     4        9      10       3

;

Contributor
Posts: 44

Re: Getting the Max value

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.

Super User
Posts: 3,250

Re: Getting the Max value

Posted in reply to need_sas_help

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

;

Contributor
Posts: 44

Re: Getting the Max value

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

Super User
Posts: 5,495

Re: Getting the Max value

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 valueSmiley Happy;

But the problem needs a little more clarity on what is actually required.


Contributor
Posts: 44

Re: Getting the Max value

Posted in reply to Astounding

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

Super User
Posts: 3,250

Re: Getting the Max value

Posted in reply to need_sas_help

%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;

Contributor
Posts: 44

Re: Getting the Max value

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?

Super User
Posts: 19,767

Re: Getting the Max value

Posted in reply to need_sas_help

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;

Contributor
Posts: 44

Re: Getting the Max value

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

Super User
Posts: 19,767

Re: Getting the Max value

Posted in reply to need_sas_help

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);

Contributor
Posts: 44

Re: Getting the Max value

You are not only smart but also very helpful. You saved me a few times. Thanks a lot


Super Contributor
Posts: 298

Re: Getting the Max value

Posted in reply to need_sas_help

data need;

   set have;

   value = max(of value1 - value4);

run;

PROC Star
Posts: 7,467

Re: Getting the Max value

Posted in reply to need_sas_help

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

;

Ask a Question
Discussion stats
  • 15 replies
  • 458 views
  • 0 likes
  • 8 in conversation