DATA Step, Macro, Functions and more

How to Return Each Value in Temporary Array

Accepted Solution Solved
Reply
Contributor
Posts: 46
Accepted Solution

How to Return Each Value in Temporary Array

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

 

 


Accepted Solutions
Solution
‎08-30-2017 02:51 PM
PROC Star
Posts: 1,282

Re: How to Return Each Value in Temporary Array

[ Edited ]
Posted in reply to acemanhattan

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 

View solution in original post


All Replies
Solution
‎08-30-2017 02:51 PM
PROC Star
Posts: 1,282

Re: How to Return Each Value in Temporary Array

[ Edited ]
Posted in reply to acemanhattan

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 

Contributor
Posts: 46

Re: How to Return Each Value in Temporary Array

Posted in reply to novinosrin

@novinosrin


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

 

PROC Star
Posts: 1,282

Re: How to Return Each Value in Temporary Array

[ Edited ]
Posted in reply to acemanhattan

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 Smiley Happy 

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;

Contributor
Posts: 46

Re: How to Return Each Value in Temporary Array

Posted in reply to novinosrin

Ah, too easy! Thanks.

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 183 views
  • 0 likes
  • 2 in conversation