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.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.