BookmarkSubscribeRSS Feed
SASnewb789
Calcite | Level 5

Hi everyone! I'm working my way through the SAS Programming 2: Data Manipulation Techniques course. I noticed in the Loops part of the course that some of the expressions in the Do loop require an equal sign and some don't. For example, in Practice p206p04.sas the code reads:

 

data IncreaseDayVisits;  
    set pg2.np_summary;
    where Reg='NE' and DayVisits<100000;
    IncrDayVisits=DayVisits;
    Year=0;
	do while (IncrDayVisits<100000);
    	IncrDayVisits=IncrDayVisits*1.06;
    	Year+1;
	end;
    format IncrDayVisits comma12.;
    keep ParkName DayVisits IncrDayVisits Year;
run;

The lines in the above code in blue are both formula expressions, however the first formula requires an assignment of IncrDayVisits= and the second formula doesn't require an assignment with an equal sign. 

 

Can anyone clarify what the difference is? Why does one need an assignment statement with an equal sign and the other does not?

 

Thanks!

1 REPLY 1
SASJedi
SAS Super FREQ

This is an assignment statement:

IncrDayVisits=IncrDayVisits*1.06;

The expression to the right of = is resolved, and the result is assigned to the variable IncrDayVisits. The value of IncrDayVisits will be reinitialized to missing for each iteration of the data step, and if IncrDayVisits is missing when the expression is evaluated, the result of the expression will also be missing.

 

This is a SUM statement:

Year+1;

The variable to the left of + is an accumulator variable. Accumulator variable values are not automatically reinitialized when the DATA step iterates - the values are instead retained across iterations. After the expression to the right of + is evaluated, and the result is summed to the existing value of the accumulator variable (the equivalent of Year=SUM(Year,1), in this case). Summing will ignore missing values, so if Year is missing, the resulting value of Year is 1.
 

 

Check out my Jedi SAS Tricks for SAS Users

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 1 reply
  • 623 views
  • 1 like
  • 2 in conversation