BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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!!
5 REPLIES 5
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
deleted_user
Not applicable
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.
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Yes - sorry about that. You've got it right.

Scott Barry
SBBWorks, Inc.
ChrisNZ
Tourmaline | Level 20
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;
deleted_user
Not applicable
Many thanks Chris! This works great and is much cleaner than the two loops I had before.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 698 views
  • 0 likes
  • 3 in conversation