DATA Step, Macro, Functions and more

Can I add a blank row among pre-existing rows?

Reply
Occasional Contributor
Posts: 5

Can I add a blank row among pre-existing rows?

What I would like to do is simply illustrated below:

 

The current data set is,

 

(line number) ID VALUE

 

(1)                  A  90

(2)                  B  90 

(3)                  C  83 

(4)                  D  49

(5)                  E  20

 

What I would like to do add a blank line between third and fourth rows like this:

(line number) ID VALUE

(1)                  A  90

(2)                  B  90 

(3)                  C  83 

(4)

(5)                  D  49

(6)                  E  20

 

I would like to know the simplest way to get the new dataset. Thanks in advance.

 

 

Super User
Posts: 17,758

Re: Can I add a blank row among pre-existing rows?

Do you need this as a dataset or part of a final report?
Occasional Contributor
Posts: 5

Re: Can I add a blank row among pre-existing rows?

At this moment, I actually need this for my final output. I would like to handle the final format in SAS as possible as I can!

 

 

Trusted Advisor
Posts: 1,367

Re: Can I add a blank row among pre-existing rows?

what is your code to create your output ?

what are your criteria to add a blank row on output ?

 

if you use proc print - you may need add such row(s) to the input dataset.

 

if you use put statement in a data step, then you can add a blank row by:

     put ... your variables ... /;

 

if you use proc report you need define break conditions.

 

Super User
Posts: 5,075

Re: Can I add a blank row among pre-existing rows?

Here's how to add a blank observation:

 

data want;

set have;

output;

if _n_=3;

id = ' ';

value = .;

output;

run;

 

That assumes VALUE is a numeric variable.  For printing purposes, you may want to add this option:

 

options missing=' ';

 

Otherwise, missing numerics print as a dot.  As others have implied, there may be other approaches that do a better job.  It depends on the ultimate goal.

Super User
Super User
Posts: 6,498

Re: Can I add a blank row among pre-existing rows?

Create a new variable that you can use to indicate where to put the blank line. Perhaps a grouping variable?

 

data have;
  input id $ value ;
cards;
 A  90
 B  90
 C  83
 D  49
 E  20
;

data report;
  set have ;
  if 'A' <= ID  <= 'C' then group=1 ;
  else group=2;
run;

proc report nofs data=report headline ;
  columns group id value ;
  define group / order noprint ;
  define id / order ;
  define value / display ;
  break after group / skip ;
run;

 

 

Occasional Contributor
Posts: 5

Re: Can I add a blank row among pre-existing rows?

Thank you for all your comments.

I think I gave you some confusion. What I would like to do is exaclty for my output, but I didn't mean that I would use "proc report", "proc print", etc.

 

I manually make an output using proc sql and data step. In the middle of amending the customized output format, I realized that I should have a blank between some rows, so I would like to ask you for it. 

Super User
Posts: 17,758

Re: Can I add a blank row among pre-existing rows?

@Anonym432 @Astounding Solution is what your looking for then. 

 

 

You dont specify if this is missing for specific rows or a random designation. If there's a logic behind missing rows then there may be other ways. 

Super User
Super User
Posts: 7,392

Re: Can I add a blank row among pre-existing rows?

So what is the logical indicator that there should be a blank between 3 and 4?  I mean why not a blank between B and C?  If its just that after C you want a blank:

data want;
  set have;
  if id="C" then do;
    output;
    id="";
    output;
  end;
  else output;
run;
Trusted Advisor
Posts: 1,367

Re: Can I add a blank row among pre-existing rows?

post your code to make it short and more efficient.

 

see my previous post: "what is your code ...:

Occasional Contributor
Posts: 5

Re: Can I add a blank row among pre-existing rows?

Thanks again. I would like to add one complete empty row!

 

Another example is that, I have a data set with 10,000 rows and 5 columns (Let's say, Column1 - Column 5).

 

By addiing one empty column between two lines 3500 and 3501, I would like to change my dataset 10,001 lines with 5 columns. In this case, the empty line should be 3501th. 

 

Thanks!

 

 

Super User
Posts: 5,075

Re: Can I add a blank row among pre-existing rows?

You can change the number of variables or the number of observations, but the tools remain the same.  Here's a version that applies to 5 variables and 10,000 observations, and adds a blank observation between the 3500th and 3501st:

 

data want;

set have;

output;

if _n_=3500;

call missing (of column1-column5);

output;

run;

 

Is this the final version of what needs to be solved?

Super User
Posts: 17,758

Re: Can I add a blank row among pre-existing rows?

Not sure if this would work but what I'd try. 

 

Output;

If _n_ = 3500 then do;

Call missing(of _all_);

output;

end;

Super User
Super User
Posts: 7,392

Re: Can I add a blank row among pre-existing rows?

Two things I would say here:

First, using observation number (3000 or whatever) is rarely a good idea, as any amount of changes to the dataset will invalidate this programming - e.g. sort, extra rows, different processing, change to data etc.  Logical assignment should be used.

Secondly, your creating a report with 10000 observations?  That really isn't using your time to any great benefit, nobody will ever look at a report with 10000 observations in it.  What is the data, why is it not summarised?  

It makes far more sense to summarise your data, and create nice outputs based on the summaries.

Ask a Question
Discussion stats
  • 13 replies
  • 1244 views
  • 2 likes
  • 6 in conversation