BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ttxq
Fluorite | Level 6

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

1 ACCEPTED SOLUTION

Accepted Solutions
WarrenKuhfeld
Ammonite | Level 13

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

5 REPLIES 5
WarrenKuhfeld
Ammonite | Level 13

dsn + 1;

is a sum statement.  It implicitly adds:

retain dsn 0;

 

dsn = dsn + 1;

is an assignment statement.  There is no implicit retain.

ballardw
Super User

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.

ttxq
Fluorite | Level 6
I see. Thank you
Reeza
Super User

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;
ttxq
Fluorite | Level 6
Thanks a lot

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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