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

Hi all,

 

I have been working with this data set and finally got an Array to work, but I wanted to change it to include both pre and post data. As such, I modified my code from:

 

 

data Tall;
	set infl.arc_inflation_jb_102618;
	array _month(12) N_PreResYr1stM_House
		N_PreResYr2ndM_House
		N_PreResYr3rdM_House
		N_PreResYr4thM_House
		N_PreResYr5thM_House
		N_PreResYr6thM_House
		N_PreResYr7thM_House
		N_PreResYr8thM_House
		N_PreResYr9thM_House
		N_PreResYr10thM_House
		N_PreResYr11thM_House
		N_PreResYr12thM_House;


	do i=1 to dim(_month);
		interval='Month';
		Month = i;
			PreResYr_House = _month(i);	
		output;
	end;
run;

This code was working fine at giving me the 12 months split up.

 

 

to this: 

data Tall;
	set infl.arc_inflation_jb_102618;
	array _month(2,12) N_PreResYr1stM_House
		N_PreResYr2ndM_House
		N_PreResYr3rdM_House
		N_PreResYr4thM_House
		N_PreResYr5thM_House
		N_PreResYr6thM_House
		N_PreResYr7thM_House
		N_PreResYr8thM_House
		N_PreResYr9thM_House
		N_PreResYr10thM_House
		N_PreResYr11thM_House
		N_PreResYr12thM_House
	N_PostResYr1stM_House
		N_PostResYr2ndM_House
		N_PostResYr3rdM_House
		N_PostResYr4thM_House
		N_PostResYr5thM_House
		N_PostResYr6thM_House
		N_PostResYr7thM_House
		N_PostResYr8thM_House
		N_PostResYr9thM_House
		N_PostResYr10thM_House
		N_PostResYr11thM_House
		N_PostResYr12thM_House;

	do i=1 to dim(_month);
		interval='Month';
		Month = i;
			PreResYr_House = _month(1,i);
PostResYr_House = _month(2,i);	
		output;
	end;
run;

 Now I cannot figure out why this is not giving me my 12 months still with both pre and post but is producing only 2 rows with the correct values for just months 1 and 2?

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

use

do i=1 to dim2(_month);

And still

this piece of logic seems incorrect to me

 

PreResYr_House = _month(1,i);
PostResYr_House = _month(2,i);	

 as you are merely overwriting

 

Are you trying to do

do i=1 to dim(_month);
		interval='Month';
		Month = i;
			PreResYr_House = _month(1,i);
output; PostResYr_House = _month(2,i); output; end;

View solution in original post

9 REPLIES 9
Reeza
Super User
What does DIM return when you have a two dimensional array? The first or second dimension? Since it's two I'm going to guess the first. Look up how to find the other dimensions in the docs 😉
joebacon
Pyrite | Level 9

Thank you for always being patient and helping me learn! You're seriously the best.

novinosrin
Tourmaline | Level 20

use

do i=1 to dim2(_month);

And still

this piece of logic seems incorrect to me

 

PreResYr_House = _month(1,i);
PostResYr_House = _month(2,i);	

 as you are merely overwriting

 

Are you trying to do

do i=1 to dim(_month);
		interval='Month';
		Month = i;
			PreResYr_House = _month(1,i);
output; PostResYr_House = _month(2,i); output; end;
Reeza
Super User
@novinosrin I think other than dim2 the code is likely what the OP wants, since it will create pre/post beside each other which makes it easier to calculate differences. For a full 'tidy' data set though it should be the way you've suggested.
novinosrin
Tourmaline | Level 20

True at 4 pm and I am already tired. Will have to wait for OP to give us a sample of what he/she has and the requirement

joebacon
Pyrite | Level 9

The first bit of code was what i needed and then I reorganized it in a really long, terrible way. However, when i got the second bit of code, it helped me learn some more about how SAS processes. Thanks for that. Cool little trick to put them side by side like Reeza said.

 

Thank you!

novinosrin
Tourmaline | Level 20

We are glad. 🙂 Have a good day!

ballardw
Super User

@joebacon wrote:

Hi all,

 

I have been working with this data set and finally got an Array to work, but I wanted to change it to include both pre and post data. As such, I modified my code from:

 

 

data Tall;
	set infl.arc_inflation_jb_102618;
	array _month(12) N_PreResYr1stM_House
		N_PreResYr2ndM_House
		N_PreResYr3rdM_House
		N_PreResYr4thM_House
		N_PreResYr5thM_House
		N_PreResYr6thM_House
		N_PreResYr7thM_House
		N_PreResYr8thM_House
		N_PreResYr9thM_House
		N_PreResYr10thM_House
		N_PreResYr11thM_House
		N_PreResYr12thM_House;


	do i=1 to dim(_month);
		interval='Month';
		Month = i;
			PreResYr_House = _month(i);	
		output;
	end;
run;

This code was working fine at giving me the 12 months split up.

 

 

to this: 

data Tall;
	set infl.arc_inflation_jb_102618;
	array _month(2,12) N_PreResYr1stM_House
		N_PreResYr2ndM_House
		N_PreResYr3rdM_House
		N_PreResYr4thM_House
		N_PreResYr5thM_House
		N_PreResYr6thM_House
		N_PreResYr7thM_House
		N_PreResYr8thM_House
		N_PreResYr9thM_House
		N_PreResYr10thM_House
		N_PreResYr11thM_House
		N_PreResYr12thM_House
	N_PostResYr1stM_House
		N_PostResYr2ndM_House
		N_PostResYr3rdM_House
		N_PostResYr4thM_House
		N_PostResYr5thM_House
		N_PostResYr6thM_House
		N_PostResYr7thM_House
		N_PostResYr8thM_House
		N_PostResYr9thM_House
		N_PostResYr10thM_House
		N_PostResYr11thM_House
		N_PostResYr12thM_House;

	do i=1 to dim(_month);
		interval='Month';
		Month = i;
			PreResYr_House = _month(1,i);
PostResYr_House = _month(2,i);	
		output;
	end;
run;

 Now I cannot figure out why this is not giving me my 12 months still with both pre and post but is producing only 2 rows with the correct values for just months 1 and 2?


From the first post I and following I was thinking that doing one array per topic would be the way the go. then you have parallel code and copy  paste and edit actual gets moderately easy. You have also discovered whey SAS programmers tend to place iterated values at the end of a variable name and not in the middle. Arrays are much easy to work with when you have N_PreResYrM_House1 to N_PreResYrM_House12 as the array statement would be : array a N_PreResYrM_House1 - N_PreResYrM_House12 ;

Personally I would rename your variables just to make the arrays simpler. I think if you search the forum you may find an example of PROC DATASETS used to do so with indexed variables.

1st , 2nd, 3rd , 4th do not belong in naming schemes either. Use 1 2 3 4 (except for the start of the name).

 

Consider

data example;
   set imaginarydata;
   array h var1_house var2_house var3_house;
   array c var1_car   var2_car   var3_car  ;
   array b var1_boat  var2_boat  var3_boat ;
   do month=1 to dim(h);
      house = h[month];
      car   = c[month];
      boat  = b[month];
      output;
   end;
   drop var: ;
run;

Pick short names for arrays as practical. If your groups of variables only vary by a few characters such as your PRERES vs POSTRES

 

get one of these arrays setup and you can copy and select and replace in selection in the editor to make the 2nd (3rd 4th etc) similar arrays.

Then the code inside loop for the different topics is copy and paste similarly.

While SAS does support multiple dimension arrays I doubt if the complexity is warranted here.

joebacon
Pyrite | Level 9

Thank you! Obviously I am very new and trying to learn. This helped break it down. I appreciate it.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 9 replies
  • 786 views
  • 8 likes
  • 4 in conversation