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?
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;
Thank you for always being patient and helping me learn! You're seriously the best.
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;
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
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!
We are glad. 🙂 Have a good day!
@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.
Thank you! Obviously I am very new and trying to learn. This helped break it down. I appreciate it.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.