DATA Step, Macro, Functions and more

Data Step behaviour

Accepted Solution Solved
Reply
Contributor
Posts: 22
Accepted Solution

Data Step behaviour

[ Edited ]

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!


Accepted Solutions
Solution
‎02-16-2017 05:57 AM
Valued Guide
Posts: 797

Re: Data Step behaviour

[ Edited ]

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

View solution in original post


All Replies
Solution
‎02-16-2017 05:57 AM
Valued Guide
Posts: 797

Re: Data Step behaviour

[ Edited ]

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
Super User
Posts: 17,819

Re: Data Step behaviour

You want this as a data set or text file?

Contributor
Posts: 22

Re: Data Step behaviour

@Reeza, yes as a data set. 

@mkeintz reply was perfect.

 

Thank you all!

☑ This topic is SOLVED.

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

Discussion stats
  • 3 replies
  • 150 views
  • 1 like
  • 3 in conversation