Hello.
Imagine this example data set:
Var_Parent Var_Child
-------------------------------
Car Ford
Car Fiat
Car Chevrolet
Animal Cat
Animal Cow
Animal Pig
Country Portugal
Country UK
Country USA
I want to copy this data set to a new one, to stay like this (literally).
As you can see, I want to OUTPUT "Begin of " every time Var_Parent changes.
Var
-------------------
Begin of Car
Ford
Fiat
Chevrolet
End of Car
Begin of Animal
Cat
Cow
Pig
End of Animal
Begin of Country
Portugal
UK
USA
End of Country
Can someone help me, please?
Thank you!
data want (keep=text);
set have;
by var_parent notsorted;
length text $24;
if first.var_parent then do;
text=catx(' ','Begin of',var_parent);
output;
end;
text=var_child;
output;
run;
if last.var_parent then do;
text=catx(' ','End of',var_parent);
output;
end;
run;
edit: forgot my notes:
Notes:
data want (keep=text);
set have;
by var_parent notsorted;
length text $24;
if first.var_parent then do;
text=catx(' ','Begin of',var_parent);
output;
end;
text=var_child;
output;
run;
if last.var_parent then do;
text=catx(' ','End of',var_parent);
output;
end;
run;
edit: forgot my notes:
Notes:
You want this as a data set or text file?
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.