BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Nietzsche
Lapis Lazuli | Level 10

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.

Nietzsche_0-1670104006557.png

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;

Nietzsche_1-1670104189570.png

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;

Nietzsche_2-1670104408414.png

 

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).

Nietzsche_0-1670104717354.png

 

 

SAS Base Programming (2022 Dec), Preparing for SAS Advanced Programming (Cancelled).
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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 

  • The values of ACCUM to be RETAINed (that is not reset to missing at the start of every iteration)
  • That WAGERATE is added to ACCUM using the SUM() function instead of normal addition so that missing values are ignored.
  • That ACCUM should be initialized to zero (unless you have an explicit RETAIN statement that initializes it to something else.)

That is what the SUM statement was designed for, to generate this type of cumulative sum.

View solution in original post

6 REPLIES 6
Tom
Super User Tom
Super User

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 

  • The values of ACCUM to be RETAINed (that is not reset to missing at the start of every iteration)
  • That WAGERATE is added to ACCUM using the SUM() function instead of normal addition so that missing values are ignored.
  • That ACCUM should be initialized to zero (unless you have an explicit RETAIN statement that initializes it to something else.)

That is what the SUM statement was designed for, to generate this type of cumulative sum.

Nietzsche
Lapis Lazuli | Level 10

Nietzsche_0-1670105448144.png

@Tom 

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;
SAS Base Programming (2022 Dec), Preparing for SAS Advanced Programming (Cancelled).
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
Tom
Super User Tom
Super User

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

 

Nietzsche
Lapis Lazuli | Level 10
data temp;
	set spg.usa;
	retain Accum 0;
	Accum=sum(Accum,wagerate);
run;
proc print data=temp;run;

Better?

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

 

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.

 

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

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
  • 6 replies
  • 515 views
  • 3 likes
  • 3 in conversation