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

Dear SAS experts

 

As a very simple example of my dataset, I would like to given a dataset like the following introduce a new observation as the 1st observation which only includes blanks/missing;

 

data have;
input Name $ Age num1 num2 num3;
datalines;
Maria 30 5 2 17
John 32 3 7 20
;
run;

 

It would be great to find a way to introduce the observation without having to specify "by hand" which variables are included in the dataset. I expect that I need to use some code which includes "then do" and "call missing (of _all_)", but I a cannot reach the desired code.

 

Can someone help?

 

Thank you

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

It's even easier:

data want;
output;
set have;
run;

 

Edit:

Explanation for @mgrasmussen:

  1. In the first iteration of the DATA step the OUTPUT statement before the SET statement creates the blank observation, as desired. The variables from dataset HAVE exist in the program data vector due to the SET statement from the compilation phase, but have not been populated with values yet.
  2. In the second (third, ...) iteration of the DATA step the OUTPUT statement writes the first (second, ...) observation of dataset HAVE to dataset WANT because variables read by the SET statement are automatically retained.
  3. The iteration reading the last observation from dataset HAVE is not the last iteration of the DATA step. It is the SET statement failing to read beyond the last observation which terminates the DATA step. The OUTPUT statement before that final SET statement execution thus ensures that also the last observation of HAVE is written to dataset WANT.

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

Shouldn't be hard to do, but what possible purpose is there in putting forth the effort to achieve a blank line in a data set?

--
Paige Miller
PeterClemmensen
Tourmaline | Level 20

1) Why?

 

2) How:

 

data have;
input Name $ Age num1 num2 num3;
datalines;
Maria 30 5 2 17
John 32 3 7 20
;
run;

data want;
   set have;
   
   if _N_ = 1 then do;
      call missing(of _all_);
      output;
   end;
   
   output;
run;
FreelanceReinh
Jade | Level 19

It's even easier:

data want;
output;
set have;
run;

 

Edit:

Explanation for @mgrasmussen:

  1. In the first iteration of the DATA step the OUTPUT statement before the SET statement creates the blank observation, as desired. The variables from dataset HAVE exist in the program data vector due to the SET statement from the compilation phase, but have not been populated with values yet.
  2. In the second (third, ...) iteration of the DATA step the OUTPUT statement writes the first (second, ...) observation of dataset HAVE to dataset WANT because variables read by the SET statement are automatically retained.
  3. The iteration reading the last observation from dataset HAVE is not the last iteration of the DATA step. It is the SET statement failing to read beyond the last observation which terminates the DATA step. The OUTPUT statement before that final SET statement execution thus ensures that also the last observation of HAVE is written to dataset WANT.
SASJedi
Ammonite | Level 13

I don't know WHY you would want to do this, but it's fairly simple:

data have;
/* First iteration only, write out a row before reading data */
if _n_=1 then output;
input Name $ Age num1 num2 num3;
/* Write out a row after each data read */
output;
datalines;
Maria 30 5 2 17
John 32 3 7 20
;
run;
Check out my Jedi SAS Tricks for SAS Users

sas-innovate-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1629 views
  • 7 likes
  • 6 in conversation