Hi,
i have a dataset like below
Name | Points |
x | 10 |
y | 5 |
z | 20 |
i want to add the points of x to y, (result dataset should be like below)
Name | Points |
x | 10 |
y | 15 |
z | 20 |
can any one suggest me on this.
thanks
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;
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;
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 |
x | 10 |
a | 1 |
y | 5 |
z | 20 |
i want like below.
Name | Points |
x | 10 |
a | 1 |
y | 15 |
z | 20 |
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.
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;
If there are situations with missing values, use the sum() function in place of a simple addition.
yes, i did .
but no use
@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.
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!
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.
Ready to level-up your skills? Choose your own adventure.