DATA Step, Macro, Functions and more

Indexing an array using a variable

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

Indexing an array using a variable

I am trying to use the variable PERIOD as an iterator for the period_array. However the iterator does not seem to be working. If I change period_array(PERIOD) to a fixed value location like period_array(3) then it correctly places a value at position 3. Can someone tell me where I am going wrong?

 

I have been using this as the template: https://stats.idre.ucla.edu/sas/modules/reshaping-data-long-to-wide-using-the-data-step/

 

Any assistance would be greatly appreciated,

 

Input dataset:

PATH  PERIOD  RATE
115
127
133
211
222
232

 

SAS CODE:

DATA WORK.LMM_Paths;
	SET WORK.LMM_PATHS_IN;
		BY PATH;
	ARRAY PERIOD_ARRAY(3) PERIOD1-PERIOD3;
	IF first.PATH THEN DO;
		DO i=1 to 3;
			period_array{i}=.;
		END;
	END;
	period_array(PERIOD)=RATE;
	IF last.PATH THEN OUTPUT;
RUN;

Output dataset:

 

PATHPERIODRATEPERIOD1PERIOD2PERIOD3
13605...

Accepted Solutions
Solution
‎04-13-2018 02:56 PM
PROC Star
Posts: 1,357

Re: Indexing an array using a variable

[ Edited ]
Posted in reply to mk123451243

retain the array:

DATA WORK.LMM_Paths;
	SET WORK.LMM_PATHS_IN;
		BY PATH;
	ARRAY PERIOD_ARRAY(3) PERIOD1-PERIOD3;
retain period_array; /*retain the array*/
	IF first.PATH THEN DO;
		DO i=1 to 3;
			period_array{i}=.;
		END;
	END;
	period_array(PERIOD)=RATE;
	IF last.PATH THEN OUTPUT;
RUN;

View solution in original post


All Replies
Solution
‎04-13-2018 02:56 PM
PROC Star
Posts: 1,357

Re: Indexing an array using a variable

[ Edited ]
Posted in reply to mk123451243

retain the array:

DATA WORK.LMM_Paths;
	SET WORK.LMM_PATHS_IN;
		BY PATH;
	ARRAY PERIOD_ARRAY(3) PERIOD1-PERIOD3;
retain period_array; /*retain the array*/
	IF first.PATH THEN DO;
		DO i=1 to 3;
			period_array{i}=.;
		END;
	END;
	period_array(PERIOD)=RATE;
	IF last.PATH THEN OUTPUT;
RUN;
New Contributor
Posts: 3

Re: Indexing an array using a variable

Posted in reply to novinosrin

Ah of course!

 

I see my original code was reinitializing the array at the end of the data step!

 

Thank you.

Respected Advisor
Posts: 3,834

Re: Indexing an array using a variable

Posted in reply to novinosrin

@novinosrin wrote:

retain the array:

DATA WORK.LMM_Paths;
	SET WORK.LMM_PATHS_IN;
		BY PATH;
	ARRAY PERIOD_ARRAY(3) PERIOD1-PERIOD3;
retain period_array; /*retain the array*/
	IF first.PATH THEN DO;
		DO i=1 to 3;
			period_array{i}=.;
		END;
	END;
	period_array(PERIOD)=RATE;
	IF last.PATH THEN OUTPUT;
RUN;

Consider using DO UNTIL(LAST.PATH) with SET in the loop.  This will allow you to remove the RETAIN and let SAS initialize to missing the elements of the array. 

 

data work.lmm_paths;
   do until(last.path);
      set work.lmm_paths_in;
      by path;
      array period_array[3] period1-period3;
      period_array[period]=rate;
      end;
   run;
   
☑ This topic is solved.

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

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