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:
Prod | Comp |
x | 1 |
x | 2 |
x | 3 |
y | 1 |
y | 2 |
Want:
Prod | Comp |
x | x |
x | 1 |
x | 2 |
x | 3 |
y | y |
y | 1 |
y | 2 |
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;
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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.