BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SAS_inquisitive
Lapis Lazuli | Level 10

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;
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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;

 

 

View solution in original post

2 REPLIES 2
Reeza
Super User

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;

 

 

KachiM
Rhodochrosite | Level 12
 
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;
 

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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