BookmarkSubscribeRSS Feed
Nietzsche
Lapis Lazuli | Level 10

Hi I am doing Programming 2 lesson 2 on trying to find maximum traffic count using RETAIN statement, the example code is as follow:

data cuyahoga_maxtraffic;
    set pg2.np_monthlyTraffic;
    where ParkName = 'Cuyahoga Valley NP';
    retain TrafficMax 0 MonthMax LocationMax;
    if Count>TrafficMax then do;
        TrafficMax=Count;
        MonthMax=Month;
        LocationMax=Location;
    end;
    format Count TrafficMax comma15.;
    keep Location Month Count TrafficMax MonthMax LocationMax;
run;

I am trying to understand why retain TrafficMax 0? Wouldn't that set the TrafficMax to 0 every row iteration?

for example if row 1 count is 5 and row 2 count is 4 then

TrafficMax is 0 after retain TrafficMax 0

 

then it is set to 5 after iteration of row 1, then TrafficMax is set back as 0 at the beginning of iteration of row 2, and TrafficMax is set to 4 after iteration of row 2?

 

Or am I misunderstanding the SAS compiling process, only the if/then statement is looped for each row?

 

 

 

 

SAS Base Programming (2022 Dec), Preparing for SAS Advanced Programming (Cancelled).
4 REPLIES 4
Nietzsche
Lapis Lazuli | Level 10

Also adding the 0 in the RETAIN statement does not seem to affect TrafficMax or MonthMax but LocationMax becomes empty. Why is that?

SAS Base Programming (2022 Dec), Preparing for SAS Advanced Programming (Cancelled).
Kurt_Bremser
Super User

By adding the zero in the RETAIN statement, you define the variable as numeric, so it can't hold the character values of location.

Without the zero, the type of the variable is set later during compilation when the assignment statement is encountered.

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
  • 525 views
  • 0 likes
  • 2 in conversation