Fluorite | Level 6

## Copying values to an array of different size

Hi, everyone!

I have a dataset that looks like this:

``````data have;
input ID TIME PA0 PA1 PA2 PA3 PA4 PA5 PA6 PA7 PA8 PA9 PA10 PA11 PA12 PA13 PA14 PA15 PA16 PA17 PA18 PA19 PA20 PA21 PA22 PA23;
cards;
1 12 1 1 1 1 0 0 1 1 0 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2
2 6 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
3 23 1 0 1 0 1 2 1 1 0 1 1 1 1 2 2 2 0 1 1 0 0 0 0 0
;``````

Basically, each row represents a patient that has a time of follow-up (in years) and an array of PA0-PA23 which represent levels of physical activity during 24 years.

What I need is a new array of physical activity that is exactly this long (24 columns) and has the exact same values in each column as the PA array, BUT it generates missing values after reaching the value of time. For instance, for the first patient, the array has the same values from PA0 to PA11 but starts putting missing values from PA12 onwards:

1 12 1 1 1 1 0 0 1 1 0 1 2 2 2 . . . . . . . . . . . .

Can you tell me how to create this second array?

Thanks 🙂

1 ACCEPTED SOLUTION

Accepted Solutions
Super User

## Re: Copying values to an array of different size

``````array new_pa(24) new_pa1-new_pa24;
array old_pa(24) pa1-pa24;

do i=1 to time;
new_pa(i) = old_pa(i);
end;

``````

You can set the loop using a variable, in this case time. So, either set all to the same values and then assign missing from time=13 to 24 or you could initialize all to missing and only copy over up to 12. I show the latter in the code above.

2 REPLIES 2
Super User

## Re: Copying values to an array of different size

``````array new_pa(24) new_pa1-new_pa24;
array old_pa(24) pa1-pa24;

do i=1 to time;
new_pa(i) = old_pa(i);
end;

``````

You can set the loop using a variable, in this case time. So, either set all to the same values and then assign missing from time=13 to 24 or you could initialize all to missing and only copy over up to 12. I show the latter in the code above.

Opal | Level 21

## Re: Copying values to an array of different size

Did you show what you are currently getting or what you want to get? If it's the latter, go with @Reeza'a suggested code (other than, of course, load the old array with pa0-pa23).

However, if it's the former, and the number of array elements changes, I'd go with something like:

```data have;
input ID TIME PA0 PA1 PA2 PA3 PA4 PA5 PA6 PA7 PA8 PA9 PA10 PA11 PA12 PA13 PA14 PA15 PA16 PA17 PA18 PA19 PA20 PA21 PA22 PA23;
cards;
1 12 1 1 1 1 0 0 1 1 0 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2
2 6 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
3 23 1 0 1 0 1 2 1 1 0 1 1 1 1 2 2 2 0 1 1 0 0 0 0 0
;
data _null_;
set have (obs=1);
length forexec \$255;
array havepa(*) pa:;
call execute('data want(drop=i);set have;array havepa(*) pa:;');
forexec=catx(' ','array wantpa(',dim(havepa),');do i=1 to',dim(havepa),';');
call execute(forexec);
call execute('wantpa(i)=havepa(i);end;run;');
run;
```

I wish there was a way to use the dim function, directly, to create new arrays of a given size but, if there is, I'm not aware of it.

Art, CEO, AnalystFinder.com

Discussion stats
• 2 replies
• 757 views
• 2 likes
• 3 in conversation