DATA Step, Macro, Functions and more

Adding Row by Group

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

Adding Row by Group

[ Edited ]

I'm looking to take the first row of each group in a table and create an additional row that has the value from the first column (prod) in the second column (comp). Basically, this is an unbundling of a product, where a product ID has components attached to it, but the actual product ID itself should also be considered part of the bundle (one of the components) when joining to other tables.

Have:

ProdComp
x1
x2
x3
y1
y2

 

Want:

ProdComp
xx
x1
x2
x3
yy
y1
y2

Accepted Solutions
Solution
‎03-09-2016 06:34 PM
Super User
Posts: 17,785

Re: Adding Row by Group

Use BY processing combined with an explicit output statement. You may have to tweak this if you want to maintain the order, and if you want to include character and numeric values in a single column - it will have to be a character variable.

 

data want;
set have;
by prod;

if first.prod then do;
output;
comp=prod;
output;
end;
else output;
run;

View solution in original post


All Replies
Solution
‎03-09-2016 06:34 PM
Super User
Posts: 17,785

Re: Adding Row by Group

Use BY processing combined with an explicit output statement. You may have to tweak this if you want to maintain the order, and if you want to include character and numeric values in a single column - it will have to be a character variable.

 

data want;
set have;
by prod;

if first.prod then do;
output;
comp=prod;
output;
end;
else output;
run;
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 1 reply
  • 185 views
  • 2 likes
  • 2 in conversation