The code I have below takes the current and previous 7 observations of INV_CR_t and returns the higest, lowest, and Sum of the values. For troubleshooting purposes I'd like to be able to see each value from the array on every observation, not just the min and max values. Is there anything I can do to update this code
data Want;
array p{0:7} _temporary_;
set have;
by STATE PlAN LAG;
p{mod(_n_,8)} = var_8;
min= min(of p{*});
max= max(of p{*});
Sum = sum(of p{*});
run;
To go from data that looks like this
State | Plan | Lag | Var1 |
OR | a | 0 | 32 |
OR | a | 0 | 43 |
OR | a | 0 | 46 |
OR | a | 0 | 69 |
OR | a | 0 | 12 |
OR | a | 0 | 25 |
OR | a | 0 | 39 |
OR | a | 0 | 64 |
OR | a | 0 | 65 |
OR | a | 0 | 8 |
OR | a | 0 | 67 |
OR | a | 0 | 12 |
OR | a | 0 | 58 |
OR | a | 0 | 5 |
OR | a | 0 | 23 |
OR | a | 0 | 39 |
OR | a | 0 | 50 |
OR | a | 0 | 57 |
OR | a | 0 | 27 |
OR | a | 0 | 41 |
OR | a | 0 | 16 |
OR | a | 0 | 27 |
OR | a | 0 | 49 |
OR | a | 0 | 34 |
OR | a | 0 | 41 |
To data that looks like this (it doesn't matter to me whether the variables descend from first to last observation or otherwise):
State | Plan | Lag | Var1 | Var2 | Var3 | Var4 | Var5 | Var6 | Var7 | Var8 | Min | Max | Sum |
OR | a | 0 | 32 | ||||||||||
OR | a | 0 | 43 | ||||||||||
OR | a | 0 | 46 | ||||||||||
OR | a | 0 | 69 | ||||||||||
OR | a | 0 | 12 | ||||||||||
OR | a | 0 | 25 | ||||||||||
OR | a | 0 | 39 | ||||||||||
OR | a | 0 | 64 | 39 | 25 | 12 | 69 | 46 | 43 | 32 | 12 | 69 | 266 |
OR | a | 0 | 65 | 64 | 39 | 25 | 12 | 69 | 46 | 43 | 12 | 69 | 298 |
OR | a | 0 | 8 | 65 | 64 | 39 | 25 | 12 | 69 | 46 | 8 | 69 | 320 |
OR | a | 0 | 67 | 8 | 65 | 64 | 39 | 25 | 12 | 69 | 8 | 69 | 282 |
OR | a | 0 | 12 | 67 | 8 | 65 | 64 | 39 | 25 | 12 | 8 | 67 | 280 |
OR | a | 0 | 58 | 12 | 67 | 8 | 65 | 64 | 39 | 25 | 8 | 67 | 280 |
OR | a | 0 | 5 | 58 | 12 | 67 | 8 | 65 | 64 | 39 | 5 | 67 | 313 |
OR | a | 0 | 23 | 5 | 58 | 12 | 67 | 8 | 65 | 64 | 5 | 67 | 279 |
OR | a | 0 | 39 | 23 | 5 | 58 | 12 | 67 | 8 | 65 | 5 | 67 | 238 |
OR | a | 0 | 50 | 39 | 23 | 5 | 58 | 12 | 67 | 8 | 5 | 67 | 212 |
OR | a | 0 | 57 | 50 | 39 | 23 | 5 | 58 | 12 | 67 | 5 | 67 | 254 |
OR | a | 0 | 27 | 57 | 50 | 39 | 23 | 5 | 58 | 12 | 5 | 58 | 244 |
OR | a | 0 | 41 | 27 | 57 | 50 | 39 | 23 | 5 | 58 | 5 | 58 | 259 |
OR | a | 0 | 16 | 41 | 27 | 57 | 50 | 39 | 23 | 5 | 5 | 57 | 242 |
OR | a | 0 | 27 | 16 | 41 | 27 | 57 | 50 | 39 | 23 | 16 | 57 | 253 |
OR | a | 0 | 49 | 27 | 16 | 41 | 27 | 57 | 50 | 39 | 16 | 57 | 257 |
OR | a | 0 | 34 | 49 | 27 | 16 | 41 | 27 | 57 | 50 | 16 | 57 | 267 |
OR | a | 0 | 41 | 34 | 49 | 27 | 16 | 41 | 27 | 57 | 16 | 57 | 251 |
Do you mean you wanna keep the array elements?
data Want;
array p{0:7} p0-p7 ;
retain p;
set have;
by STATE PlAN LAG;
p{mod(_n_,8)} = var_8;
min= min(of p{*});
max= max(of p{*});
Sum = sum(of p{*});
run;
See if this helps,
HTH
Do you mean you wanna keep the array elements?
data Want;
array p{0:7} p0-p7 ;
retain p;
set have;
by STATE PlAN LAG;
p{mod(_n_,8)} = var_8;
min= min(of p{*});
max= max(of p{*});
Sum = sum(of p{*});
run;
See if this helps,
HTH
This is exactly what I needed! Thanks.
Is there a simple way to get the variables listed at the far right of my data, instead of on the left
Well, of course yes. Basically, you simply need to allow the compiler to compile your input dataset variables before compiling array elements as it constructs the PDV. And so how do you do that-
Swap 🙂
data Want;
set have;
by STATE PlAN LAG;
array p{0:7} p0-p7 ; retain p;
p{mod(_n_,8)} = var_8;
min= min(of p{*});
max= max(of p{*});
Sum = sum(of p{*});
run;
Ah, too easy! Thanks.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.