BookmarkSubscribeRSS Feed
need_sas_help
Calcite | Level 5

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.

15 REPLIES 15
stat_sas
Ammonite | Level 13

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

;

need_sas_help
Calcite | Level 5

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.

SASKiwi
PROC Star

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

;

need_sas_help
Calcite | Level 5

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

Astounding
PROC Star

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.


need_sas_help
Calcite | Level 5

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

SASKiwi
PROC Star

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

need_sas_help
Calcite | Level 5

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?

Reeza
Super User

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;

need_sas_help
Calcite | Level 5

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

Reeza
Super User

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

need_sas_help
Calcite | Level 5

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


KachiM
Rhodochrosite | Level 12

data need;

   set have;

   value = max(of value1 - value4);

run;

art297
Opal | Level 21

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 15 replies
  • 2414 views
  • 0 likes
  • 8 in conversation