DATA Step, Macro, Functions and more

How to insert empty row under conditions

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 115
Accepted Solution

How to insert empty row under conditions

Hi there:

i have this data set composed by lines and some characters, here i need to create empty lines under each two rows

 

data have;

input ID A$ B$ C$ D$ E$ F$ G$;

cards;

1 255 341 154 238 ? 253 134 117 116 ?
1 ? ? 154 238 ? 253 134 125 116 ?
2 255 335 154 238 101 239 134 117 116 107
2 ? ? 154 250 101 253 134 125 116 115
3 255 319 154 238 101 239 134 117 116 107
3 ? ? 154 238 109 253 134 125 116 115

;

 

 what i need is:

 

  

 #1                  
255 341 154 238 ? 253 134 117 116 ?
? ? 154 238 ? 253 134 125 116 ?
#2                   
255 335 154 238 101 239 134 117 116 107
? ? 154 250 101 253 134 125 116 115
 #3                  
255 319 154 238 101 239 134 117 116 107
? ? 154 238 109 253 134 125 116 115

 

 

Thank you very much


Accepted Solutions
Solution
‎01-30-2018 12:20 PM
PROC Star
Posts: 1,767

Re: How to insert empty row under conditions

[ Edited ]
Posted in reply to jonatan_velarde

slight adjustement

 

data want;
if 0 then set have;
array t(*) a--g;
if _n_=1 then do;call missing(of t(*));A=cats('#','1');output;end;
set have;
by id;
output;
if last.id then do;call missing(of t(*));A=cats('#',id+1);output;end;
drop id;
run;

 

View solution in original post


All Replies
Super User
Posts: 23,663

Re: How to insert empty row under conditions

Posted in reply to jonatan_velarde

PROC REPORT can do that for reports that are printed, I'm assuming it's not a data set you want?

 

https://communities.sas.com/t5/ODS-and-Base-Reporting/Adding-a-Blank-Line-in-Proc-Report-Output/td-p...

 

 

Frequent Contributor
Posts: 115

Re: How to insert empty row under conditions

Nice option, already saw this one, but my doubt was how to add the "#n" before and after each ID
Super User
Posts: 23,663

Re: How to insert empty row under conditions

Posted in reply to jonatan_velarde

I'd use a Data _null_ step then with explict PUT statements which would easily do that. 


jonatan_velarde wrote:
Nice option, already saw this one, but my doubt was how to add the "#n" before and after each ID

But it's not what your question was - 'How to insert empty row under conditions' was what you asked, so that's what I answered.

 

 

 

proc sort data=sashelp.class out=class;
by age;
run;

data _null_;
set class;
by age;

if first.age then do;
put 'Age #' age;
end;

put name sex weight height;
run;


PROC Star
Posts: 1,767

Re: How to insert empty row under conditions

Posted in reply to jonatan_velarde
data have;
input ID A$ B$ C$ D$ E$ F$ G$;
cards;
1 255 341 154 238 ? 253 134 117 116 ?
1 ? ? 154 238 ? 253 134 125 116 ?
2 255 335 154 238 101 239 134 117 116 107
2 ? ? 154 250 101 253 134 125 116 115
3 255 319 154 238 101 239 134 117 116 107
3 ? ? 154 238 109 253 134 125 116 115
;

data want;
if 0 then set have;
array t(*) a--g;
if _n_=1 then do; call missing(of t(*));output;end;
set have;
by id;
output;
if last.id then do;call missing(of t(*));output;end;
run;
Super User
Posts: 23,663

Re: How to insert empty row under conditions

Posted in reply to novinosrin

@novinosrin is better because it creates a data set that you can use with PROC PRINT to any ODS output, such as PDF or Excel. Mine will work for text files and such but it's more cumbersome as well. 

Frequent Contributor
Posts: 115

Re: How to insert empty row under conditions

Is a nice options and very usefull, but is missing the "#n" after and before ID.
Solution
‎01-30-2018 12:20 PM
PROC Star
Posts: 1,767

Re: How to insert empty row under conditions

[ Edited ]
Posted in reply to jonatan_velarde

slight adjustement

 

data want;
if 0 then set have;
array t(*) a--g;
if _n_=1 then do;call missing(of t(*));A=cats('#','1');output;end;
set have;
by id;
output;
if last.id then do;call missing(of t(*));A=cats('#',id+1);output;end;
drop id;
run;

 

Frequent Contributor
Posts: 115

Re: How to insert empty row under conditions

Posted in reply to novinosrin
Awesome!!!
☑ This topic is solved.

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

Discussion stats
  • 8 replies
  • 426 views
  • 3 likes
  • 3 in conversation