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

Hi,

 

i have a dataset like below

Name Points
x10
y5
z20

 

i want to add the points of x to y, (result dataset should be like below)

Name Points
x10
y15
z20

 

 

can any one suggest me on this.

 

thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Then we do not use lag(), but a retained variable:

data want;
set have;
retain _points;
if name = 'x' then _points = points;
if name = 'y' then points = points + _points;
drop _:;
run;

View solution in original post

7 REPLIES 7
Kurt_Bremser
Super User
data have;
input name $ points;
datalines;
x 10
y 5
z 20
;

data want;
set have;
_points = lag(points);
if name = 'y' then points = points + _points;
drop _:;
run;
sg_kr
Obsidian | Level 7

Thanks for Rapid response.

 

this will work only when the y record is next to x.

 

if i have any other record between x and y , it wont work.

 

Name Points
x10
a1
y5
z20

 

 

i want like below.

Name Points
x10
a1
y15
z20

 

 

in my case, i dont have records next to each other, i need to capture the value of one record based on the name and summ it other record based on name.

 

thanks.

Kurt_Bremser
Super User

Then we do not use lag(), but a retained variable:

data want;
set have;
retain _points;
if name = 'x' then _points = points;
if name = 'y' then points = points + _points;
drop _:;
run;
sg_kr
Obsidian | Level 7
thanks.
but i am getting . value in the points for below line
if name = 'y' then points = points + _points;
sg_kr
Obsidian | Level 7

yes, i did .

but no use

Kurt_Bremser
Super User

@sg_kr wrote:

yes, i did .

but no use


Which only means that all arguments to the sum() function are missing. If you want to set zero in such cases, add a single zero as argument to the function.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 7 replies
  • 1293 views
  • 1 like
  • 2 in conversation