New SAS User

Completely new to SAS or trying something new with SAS? Post here for help getting started.
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
Ammonite | Level 13

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-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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
  • 726 views
  • 1 like
  • 2 in conversation