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

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 2 replies
  • 1386 views
  • 2 likes
  • 3 in conversation