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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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