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
Rhodochrosite | Level 12

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
Rhodochrosite | Level 12

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 5 replies
  • 823 views
  • 2 likes
  • 4 in conversation