BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mk123451243
Fluorite | Level 6

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

Accepted Solutions
novinosrin
Tourmaline | Level 20

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

3 REPLIES 3
novinosrin
Tourmaline | Level 20

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;
mk123451243
Fluorite | Level 6

Ah of course!

 

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

 

Thank you.

data_null__
Jade | Level 19

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