BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
rmlmrmlm
Obsidian | Level 7

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!

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

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:

  1. The BY statement generate two dummy vars, first.var_parent and last.var_parent, indicating whether the record in hand is the first or last of a by-group.
  2. The "notsorted" option tells SAS to expect the data to be grouped by var_parent, but not neccessarily in ascending order
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

3 REPLIES 3
mkeintz
PROC Star

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:

  1. The BY statement generate two dummy vars, first.var_parent and last.var_parent, indicating whether the record in hand is the first or last of a by-group.
  2. The "notsorted" option tells SAS to expect the data to be grouped by var_parent, but not neccessarily in ascending order
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Reeza
Super User

You want this as a data set or text file?

rmlmrmlm
Obsidian | Level 7

@Reeza, yes as a data set. 

@mkeintz reply was perfect.

 

Thank you all!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 3 replies
  • 775 views
  • 1 like
  • 3 in conversation