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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 7 replies
  • 966 views
  • 1 like
  • 2 in conversation