DATA Step, Macro, Functions and more

revoming blanks across columns

Reply
N/A
Posts: 0

revoming blanks across columns

I want to turn this:

var1 var2 var3 ... varX
x1............x3
.......y2.....y3
.......z2........

into this:

var1 var2 var3 ... varX
x1....x3........
y2....y3........
z2................

I want to do this for a large number of datasets and X differs for each dataset, so I want to automate the process. This is what I've written so far, where &X is a global macro variable defined in a previous step:

data temp03;
array line[&X] $ 60 ;
set temp02;

array nlines {*} line1-line&X;
do i = 1 to (&X);
if nlines(i) = "" and nmiss(of nlines(i+1)-line&X) ne (&X-i) then do;
array nlines2 {*} nlines(i)-line&X;
do j = 1 to (&X-i);
nlines2(j) = nlines2(j+1);
call missing(nlines2(j+1));
end;
end;
end;
run;

The issue I'm having results from the term "nlines(i+1)". For example, when i = 1, instead of writing "line2" it writes the actual value of the variable line2. Is there an easy way to refer to the NAME of an array element rather than its value?

Also, I'm sure there are many other ways to remove the blanks, so if you know of any please let me know.

Any help is very much appreciated!! Thank you so much!!
Super Contributor
Super Contributor
Posts: 3,174

Re: revoming blanks across columns

Posted in reply to deleted_user
Why not work from this (I would say cleaner) perspective:

DO DIM(NLINES) TO 2 BY -1;
* your variable consolidation code goes here. ;
END;

And do away with the squiggly brackets - a keyboard data-entry challenge - in favor of the much more fluid left- and right-parenthesis keystroke!


Scott Barry
SBBWorks, Inc.
N/A
Posts: 0

Re: revoming blanks across columns

Thanks for the reply.

I apologize but I am a bit confused. I agree that this perspective is cleaner, but I'm not sure how it addresses (or if it's meant to address) the issue with nlines(i+1).

Also, it's "i = dim(nlines)", right?

Thanks again, I appreciate your help.
Super Contributor
Super Contributor
Posts: 3,174

Re: revoming blanks across columns

Posted in reply to deleted_user
Yes - sorry about that. You've got it right.

Scott Barry
SBBWorks, Inc.
PROC Star
Posts: 1,760

Re: revoming blanks across columns

Posted in reply to deleted_user
Why 2 loops?
[pre]
data t;
array varr(3) $2;
input (varr1 varr2 varr3) ($);
FREEPOS=1; *position of next free slot;
do I=1 to 3;
if varr(I) ne '' then do; *there is a value to keep;
if I ne FREEPOS then do; *the value must be shifted;
varr(FREEPOS)=varr(I); *shift value;
varr(I)=''; *reset current position since value has moved;
end;
FREEPOS+1; *update position of next free slot;
end;
end;
put _ALL_;
cards;
x1 . x3
. y2 y3
. z2 .
run;
N/A
Posts: 0

Re: revoming blanks across columns

Many thanks Chris! This works great and is much cleaner than the two loops I had before.
Ask a Question
Discussion stats
  • 5 replies
  • 155 views
  • 0 likes
  • 3 in conversation