Loop in Array going to DIM-1 vs DIM

Solved
Super Contributor
Posts: 285

Loop in Array going to DIM-1 vs DIM

[ Edited ]

Hello,

I am having trouble understanding dimension of array reduced by 1.When dim of array is reduced by 1? Here is an example from SAS paper (data shift).

Time1 Time2 Time3 Time4 TIme5 Makeup

A         B         .          D        E        C

Time1 Time2 Time3 Time4 TIme5

A         B         D        E        C

``````data shift;
set score;
array apps[*] time: makeup;

do i=1 to dim(apps)-1;

if apps[i]=. then
do;

do j=i to dim(apps)-1;
apps[j]=apps[j+1];
end;
mu='-'||compress(i);
end;
end;
drop i j;
run;``````

Accepted Solutions
Solution
‎07-28-2016 06:29 PM
Super User
Posts: 23,724

Re: dim reduced by 1 in array

Posted in reply to SAS_inquisitive

You should try to understand the logic of your question first.

It looks like you're trying to move data forward if you have missing values. It doesn't matter if the last value is missing, since you can't move it anywhere, so you don't need to check that column. This means your loop can skip that column.

Commenting your code can help significantly:

``````data shift;
set score;
array apps[*] time: makeup; *Declare an array with 5 time variables and one makeup variable;

do i=1 to dim(apps)-1; *Loop over TIME variables only;

if apps[i]=. then do;  *If missing a value then;

do j=i to dim(apps)-1; *From this record and to second last;
apps[j]=apps[j+1]; *Copy records from next column forward. If it was dim(Apps) then it would go past the array since it is j+1, max of j+1 = dim(apps);
end; *End loop moving forward;
mu='-'||compress(i);
end; *End moving forward;
end; *End loop over time;
drop i j;
run;``````

All Replies
Solution
‎07-28-2016 06:29 PM
Super User
Posts: 23,724

Re: dim reduced by 1 in array

Posted in reply to SAS_inquisitive

You should try to understand the logic of your question first.

It looks like you're trying to move data forward if you have missing values. It doesn't matter if the last value is missing, since you can't move it anywhere, so you don't need to check that column. This means your loop can skip that column.

Commenting your code can help significantly:

``````data shift;
set score;
array apps[*] time: makeup; *Declare an array with 5 time variables and one makeup variable;

do i=1 to dim(apps)-1; *Loop over TIME variables only;

if apps[i]=. then do;  *If missing a value then;

do j=i to dim(apps)-1; *From this record and to second last;
apps[j]=apps[j+1]; *Copy records from next column forward. If it was dim(Apps) then it would go past the array since it is j+1, max of j+1 = dim(apps);
end; *End loop moving forward;
mu='-'||compress(i);
end; *End moving forward;
end; *End loop over time;
drop i j;
run;``````

Super Contributor
Posts: 324

Re: dim reduced by 1 in array

Posted in reply to SAS_inquisitive
`` ``
``````data shift;
set score;
array apps[*] time: makeup;

do i=1 to dim(apps)-1; * i goes 1 short if dim(apps);

if apps[i]=. then
do;        * last i can be 1 less than dim(apps) ;
* there is no use to shift the Missing value at the dim(apps) position;
do j=i to dim(apps)-1; * to use j + 1, j must be less than dim(apps);
apps[j]=apps[j+1]; * j cann't be dim(app) here;
end;
mu='-'||compress(i);
end;
end;
drop i j;
run;``````
` `

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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