Coming from python, I am trying to understand by x=x+y to reassign a variable does not work in SAS but x+y works.
I have data set spg.usa as shown below.
I simply want to create a new variable Accum in the data that adds up the WageRate variable.
Why code this code work
data temp; set spg.usa; Accum+wagerate; run; proc print data=temp;run;
but this code with x=x+y structure does not work
data temp; set spg.usa; Accum=Accum+wagerate; run; proc print data=temp;run;
but on page 145 of the prep guide, it works when assign the variable with a new value based on the original value (both sides of the equation).
If you create a variable in a data step, like your ACCUM variable, the default behavior is to set it to missing at the start of each iteration of the data step. (In a simple data step like your examples you can think of one iteration being one observation.)
So when you use a normal assignment statement like:
Accum=Accum+wagerate;
You are adding the missing value in ACCUM to the value in WAGERATE. Since normal addition with missing values results in a missing result you are setting ACCUM to a missing value on every observation.
But when you use a SUM statement like this:
Accum+wagerate;
You are saying that you want
That is what the SUM statement was designed for, to generate this type of cumulative sum.
If you create a variable in a data step, like your ACCUM variable, the default behavior is to set it to missing at the start of each iteration of the data step. (In a simple data step like your examples you can think of one iteration being one observation.)
So when you use a normal assignment statement like:
Accum=Accum+wagerate;
You are adding the missing value in ACCUM to the value in WAGERATE. Since normal addition with missing values results in a missing result you are setting ACCUM to a missing value on every observation.
But when you use a SUM statement like this:
Accum+wagerate;
You are saying that you want
That is what the SUM statement was designed for, to generate this type of cumulative sum.
Can you clarity point 3?
Aren't point 1 and 3 contradictory to each other? How can Accum be RETAINed and initialised to zero?
Anyways based on your reply
I use SUM function and RETAIN statement to get it to work.
data temp; set spg.usa; retain Accum; Accum=sum(Accum,wagerate); run; proc print data=temp;run;
Point 1 means that at the start of the operation of your DATA step, the variable is assigned a value of zero. Point 3 means that if you want it to have a value of 5.37 at the start of the data set, then you have to initialize it via a RETAIN statement.
You data step with the RETAIN statement and regular assignment statement does not assign any initial value to ACCUM. So it start with a missing value instead of starting with zero.
It works the same as the SUM statement for your data because WAGERATE did not have a missing value for the first observation.
Example:
data have;
input x @@;
cards;
. 1 2 3 4
;
data want;
set have;
sum1+x;
retain sum2;
sum2=sum(sum2,x);
retain sum3 10 ;
sum3+x;
run;
OBS x sum1 sum2 sum3 1 . 0 . 10 2 1 1 1 11 3 2 3 3 13 4 3 6 6 16 5 4 10 10 20
data temp; set spg.usa; retain Accum 0; Accum=sum(Accum,wagerate); run; proc print data=temp;run;
Better?
data work.earnings; Amount=1000; Rate=0.75/12; do month=1 to 12; Earned+(amount+earned)*rate; end; run;
if I find the above SUM statement too complicated, do you think this is an acceptabe/good substitute by breaking down the sum statement and add an extra variable?
data work.earnings; principal=1000; Rate=0.75/12; do month=1 to 12; retain earned 0; total_amount=principal+earned; earned=earned+(total_amount)*rate; output; end; run; proc print data=earnings; var principal rate month total_amount earned; run;
I just find my version is easier to understand.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: