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
;
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;
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.
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;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: