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;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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