difference between two calculation

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 10
Accepted Solution

difference between two calculation

Hi all, 

I want to know what is the differences between these two statements: (highlighted in red color)

proc sort data=com;
  by dis;
run;

data com;
  set com;
  by dis;

  if first.dis then disn+1;   (if first.dis then disn=disn+1)
run;

 

when I run each program, the value of disn is different. I want to know the difference between disn+1 and disn=disn+1.

 Thanks


Accepted Solutions
Solution
‎06-06-2017 03:19 PM
SAS Employee
Posts: 83

Re: difference between two calculation

dsn + 1;

is a sum statement.  It implicitly adds:

retain dsn 0;

 

dsn = dsn + 1;

is an assignment statement.  There is no implicit retain.

View solution in original post


All Replies
Solution
‎06-06-2017 03:19 PM
SAS Employee
Posts: 83

Re: difference between two calculation

dsn + 1;

is a sum statement.  It implicitly adds:

retain dsn 0;

 

dsn = dsn + 1;

is an assignment statement.  There is no implicit retain.

Grand Advisor
Posts: 10,233

Re: difference between two calculation

When you use a statement like this:

if first.dis then disn+1;

Then the variable DISN is created with an implied Retain statement which means the value is carried from record to record in the data step. If there is a variable in the dataset name in a SET statement then that value gets reset each time a record is pulled from the dataset.

So the statement as used will increase a count by 1 for each first level of the variable DIS when the statement executes.

 

if first.dis then disn=disn+1;

If the variable DISN is not declared with a RETAIN statement and this is the first use then the value of Disn starts as missing. When a missing value has 1 added this way the result is missing. Unless there is a variable Disn in your com set I would expect the result to be missing from the addition. If you do have a Disn variable then the result would be the addition of 1 to that value when not missing.

 

If you need to sum values when something may be missing use the SUM function. However without a retain such as your example the result of disn = sum(disn,1); would likely be 1 for every record.

Occasional Contributor
Posts: 10

Re: difference between two calculation

I see. Thank you
Grand Advisor
Posts: 17,411

Re: difference between two calculation

To make it the same, add the RETAIN, if desired.

This should make disn and disn2 the exact same.

 

data com1;
set com;
by dis;
retain disn;

if first.dis then disn=disn+1;

if first.dis then disn2+1;

run;
Occasional Contributor
Posts: 10

Re: difference between two calculation

Thanks a lot
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 93 views
  • 2 likes
  • 4 in conversation