BookmarkSubscribeRSS Feed
kyle234
Obsidian | Level 7

I want to loop through all numerical variables and find the max. I would like the results below. Thank you!

 

gpa     no of cars

3         1

4         1

5          2

 

Results I want

gpa         no of cars

5              2

4 REPLIES 4
PaigeMiller
Diamond | Level 26

This is not a loop. It is PROC SUMMARY or PROC MEANS.

 

proc means data=have max;
    var gpa no_of_cars;
run;
--
Paige Miller
FreelanceReinh
Jade | Level 19

For even greater simplicity, you can omit the VAR statement in Paige Miller's PROC MEANS step, as the procedure will then analyze all numeric variables in the input dataset.

PaigeMiller
Diamond | Level 26

@FreelanceReinh wrote:

For even greater simplicity, you can omit the VAR statement in Paige Miller's PROC MEANS step, as the procedure will then analyze all numeric variables in the input dataset.


Which may or may not be a good thing to do, if there are hundreds of variables and you only want the max of two of the variables, the VAR statement should be used.

--
Paige Miller
BrianB4233
Obsidian | Level 7

Paige's example will work just fine. Here's a couple more (I added a couple of records on my own):

 

data have;
input gpa cars;
datalines;
17 4
3 1
7 3
4 1
18 1
5 2
;
run;

** Long and unnecessarily convoluted;
data want1;
set have end=lastrow;

retain gpa_max cars_max; 

if _N_ = 1 then do; /* if 1st record in HAVE ... */
 gpa_max = gpa; /* set GPA_MAX and CARS_MAX to values of GPA & CARS */
 cars_max = cars;
end;

/* if it's not the 1st record .... */
else do;
	if gpa > gpa_max then gpa_max = gpa;
	if cars > cars_max then cars_max = cars;
end;

if lastrow; /* Output if last record in file */

keep gpa_max cars_max;
run;

** Simpler and get the same result;
proc sql;
create table want2 as select
max(gpa) as gpa_max,
max(cars) as cars_max
from have
;
quit;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 403 views
  • 2 likes
  • 4 in conversation