DATA Step, Macro, Functions and more

delete first row if blank

Reply
Super Contributor
Posts: 647

delete first row if blank

how to delete first record , if the first record is blank?
Frequent Contributor
Posts: 102

Re: delete first row if blank

In what context?
Super Contributor
Posts: 647

Re: delete first row if blank

i will get data in excel and at times the data in the second row (first row being the header they become sas variable names in sas dataset) will be empty and starting from third row the data begins.so once we have the dataset the first record will be blank.
Frequent Contributor
Posts: 102

Re: delete first row if blank

Something like this?
-------------------------------------

data stuff;
x = "A"; output;
x = " "; output;
x = "B"; output;
x = "C"; output;
run;

data stuff;
set stuff(where = (not missing(X)));
run;
Super Contributor
Posts: 647

Re: delete first row if blank

Curtis,
this is same as following right?

data stuff;
set stuff;
if x=" " then delete;
run;

say if i have address field and some of the values can be missing for address field, but not the entire record. then we cannot use either your logic or mine.

but there is a chance that first record(all fields) can be blank, then how to delete?
Frequent Contributor
Posts: 102

Re: delete first row if blank

They are almost the same except my version might be a litle faster because it wouldn't read the null record into the data step vector. This might work for you.
----------------------------------------------
data stuff;
x = "A"; y = 1; output;
x = " "; y = 1; output;
x = " "; y = .; output;
x = "B"; y = 1; output;
x = "C"; y = 1; output;
run;
data stuff;
set stuff;
if not missing(compress(cats(of _all_),'.'));
run;
Super Contributor
Posts: 647

Re: delete first row if blank

this is cool. can y be of any data type?in your example it is numeric data type.
the reason for asking for datatype is in the if condition what does '.' do in the compress function?
Frequent Contributor
Posts: 102

Re: delete first row if blank

I think this will work with any number of variables of any type.

I just had an example of one character and one numeric to show that it would work with both types. The CATS(_ALL_) will concatinate all of the variables in the data step vector into a single string. The numerics will be automatically converted to strings using a default format so the missing numerics will be a '.'. The missing characters will of course be blank. The compress( ,'.') removes those periods from the missing numerics, so if all of the values were missing all that will be left is spaces which MISSING is evalutate as true.
Super Contributor
Posts: 647

Re: delete first row if blank

it makes sense.
thanks,
Ask a Question
Discussion stats
  • 8 replies
  • 255 views
  • 0 likes
  • 2 in conversation