BookmarkSubscribeRSS Feed
kk13
Calcite | Level 5

I'm trying to create a new array called (newtopic with 5 elements or variables) where it equals non-missing topic1-topic10.  

 

data have;
input begin end topic1 - topic10;
datalines;
3 8 . . 1 0 1 0 1 . . .
2 6 . 0 0 1 1 1 . . . .
4 8 . . . 1 1 0 0 0 . .
1 5 1 1 1 1 0 . . . . .
;

data want;
input begin end newtopic1-newtopic5;
datalines;
3 8 1 0 1 0 1
2 7 0 0 1 1 1
4 9 1 1 0 0 0
1 5 1 1 1 1 0
;

3 REPLIES 3
RichardDeVen
Barite | Level 11

Use DO loop and index for large array and a second conditionally incremented index for smaller array

array x x1-x10;
array y y1-y5;

do ix = 1 to dim (x) while (iy < dim(y));
  if not missing(x(ix)) then do;
    iy = sum(iy,1);
    y(iy) = x(ix);
  end;
end;

drop ix iy;
Kurt_Bremser
Super User
data want;
set have;
array old {*} topic1-topic10;
array new {5} newtopic1-newtopic5;
do i = 1 to 5;
  new{i} = old{i + begin - 1};
end;
keep begin end newtopic:;
run;

Untested, posted from my tablet.

 

 

 

 

Ksharp
Super User
data have;
input begin end topic1 - topic10;
datalines;
3 8 . . 1 0 1 0 1 . . .
2 6 . 0 0 1 1 1 . . . .
4 8 . . . 1 1 0 0 0 . .
1 5 1 1 1 1 0 . . . . .
;
data want;
set have;
array old {*} topic1-topic10;
array new {5} newtopic1-newtopic5;
n=0;
do i = 1 to dim(old);
 if not missing(old{i}) then do;
   n+1;new{n} = old{i};
 end;
end;
keep begin end newtopic:;
run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 3 replies
  • 1276 views
  • 1 like
  • 4 in conversation