Updating variable name within a for loop while performing calculations

sas loop through variables
for loop sas
do while loop sas
sas do loop variable name
sas loop through dataset
sas macro do while loop example
nested do loops sas
sas do until (last observation)

Whenever I stumble across some sort of calculation in Python, I tend to do go for an unpythonic approach because I am not too familiar with the language:

import pandas as pd
import numpy as np

v        = 8
gf       = 2.5

data_a1  = np.random.randint(5, 10, 21)
data_a2  = np.random.randint(5, 10, 21)
data_a3  = np.random.randint(5, 10, 21)
data_a4  = np.random.randint(5, 10, 21)
data_a5  = np.random.randint(5, 10, 21)

data_b1  = np.random.randint(6, 11, 21)
data_b2  = np.random.randint(6, 11, 21)
data_b3  = np.random.randint(6, 11, 21)
data_b4  = np.random.randint(6, 11, 21)
data_b5  = np.random.randint(6, 11, 21)

e_1 = 2 * (data_a1 + data_b1) / 2 / v / gf
e_2 = 2 * (data_a2 + data_b2) / 2 / v / gf
e_3 = 2 * (data_a3 + data_b3) / 2 / v / gf
e_4 = 2 * (data_a4 + data_b4) / 2 / v / gf
e_5 = 2 * (data_a5 + data_b5) / 2 / v / gf

As you can see from the example above, I explicitly write it down five times instead of using Python how I can imagine it is intended to be used -- I would like to calculate e by updating it on every iteration using a for loop, and I would also prefer to use numpy.

Since all my effort was not bearing fruits, I turned to pandas because I was fairly confident that I could redeem myself for whatever reason:

df_a     = pd.DataFrame({'data_a1': data_a1, 'data_a2': data_a2, 'data_a3': data_a3, 'data_a4': data_a4, 'data_a5': data_a5})
df_b     = pd.DataFrame({'data_b1': data_b1, 'data_b2': data_b2, 'data_b3': data_b3, 'data_b4': data_b4, 'data_b5': data_b5})

c   = 0
dfs = []
for i,j in zip(df_a, df_b):
    e = 2 * (i + j) / 2 / v / gf
    e = e.add_suffix('_' + str(c))
    dfs.addpend(e)
    c += 1

Alas, my stupidity prevailed itself and I could not do it either way.

  • Is there a streamlined way to work with equations using numpy so that the variable is updating itself within a for loop that is considered pythonic?
  • When performing these tasks, is it recommended to stick to numpy or turn to pandas?

First let's step away from creating a lot of variable names. In Python lists can contain other objects including arrays.

datalist1 = []
for _ in range(5):
    datalist1.append(np.random.randin(5, 10, 21))
# same for datalist2
datalist2 = [np.random.randint(6, 11, 21), 
             np.random.randint(6, 11, 21),
            ...]

elist = [2*(a+b)/2/v/gf for a,b in zip(datalist1, datalist2)]

Working 2d arrays, with shape (5,21) is even better. But the kind of list iteration that I illustrate works for all of Python, not just numpy.

You could even make a list from pre existing variables:

alist = [data_b1, data_b2, ...]

7. Iteration — How to Think Like a Computer Scientist: Learning with , In Python, an assignment statement can make two variables equal, but because further call last): File "<interactive input>", line 1, in NameError: name 'x' is not defined Line 3 — updating a variable by adding 1 to it — is very common. total” of the sum so far, either on a piece of paper, in your head, or in your calculator. You probably want a dict instead of separate variables. For example. d = {} for i in range(3): d["group" + str(i)] = self.getGroup(selected, header+i) If you insist on actually modifying local variables, you could use the locals function: for i in range(3): locals()["group"+str(i)] = self.getGroup(selected, header+i)


I may be misunderstanding your intent, but from what I can tell, you aren't inexing or looking up anything, so there's no reason to go from numpy into pandas (which is just a really really well-dressed numpy array). Instead, you should be looking at the vectorized operations numpy provides.

Again, I'm not clear on your end goal, since you didn;t provide output, but is this approaching what you're after?

v        = 8
gf       = 2.5
a=np.random.randint(5,10,(21,5))
b=np.random.randint(5,10,(21,5))
c=2*(a+b)/2/v/gf

c

array([[0.9 , 0.75, 0.75, 0.6 , 0.65],
       [0.75, 0.65, 0.5 , 0.9 , 0.75],
       [0.7 , 0.6 , 0.75, 0.75, 0.85],
       [0.6 , 0.6 , 0.7 , 0.8 , 0.7 ],
       [0.6 , 0.75, 0.9 , 0.8 , 0.8 ],
       [0.85, 0.65, 0.65, 0.7 , 0.65],
       [0.65, 0.65, 0.65, 0.55, 0.7 ],
       [0.5 , 0.7 , 0.7 , 0.55, 0.6 ],
       [0.65, 0.6 , 0.8 , 0.9 , 0.7 ],
       [0.65, 0.7 , 0.55, 0.6 , 0.8 ],
       [0.75, 0.55, 0.75, 0.7 , 0.65],
       [0.8 , 0.7 , 0.65, 0.7 , 0.55],
       [0.55, 0.8 , 0.6 , 0.6 , 0.7 ],
       [0.8 , 0.75, 0.7 , 0.85, 0.7 ],
       [0.7 , 0.55, 0.75, 0.7 , 0.55],
       [0.6 , 0.7 , 0.7 , 0.6 , 0.65],
       [0.55, 0.8 , 0.7 , 0.6 , 0.75],
       [0.65, 0.75, 0.7 , 0.65, 0.6 ],
       [0.8 , 0.85, 0.7 , 0.8 , 0.7 ],
       [0.85, 0.8 , 0.55, 0.6 , 0.8 ],
       [0.8 , 0.8 , 0.75, 0.7 , 0.7 ]])

Lesson 18: Generating Data With Do Loops, That's all that the iterative DO loop in the DATA step is telling SAS to do. value of the current iteration of the DO loop, can be any valid SAS variable name. Here's what the program data vector looks like with the updated Investment value : The following program uses a DO UNTIL loop to perform the calculation for us :. Once you understand how the FOR loop is interpreted, you'll know how to circumvent its limitations. First some hands-on training: exercise 1. OK, what did we just find out? It seems we can not change a variable within a FOR loop and use that changed variable in the same loop. Can't we? Yes we can! What we need is delayed variable expansion.


So, given additional information, what about this:

#simulate getting new data every day for a week
n_days   = 7

#set constants
v        = 8
gf       = 2.5
data_dict={}
#append data
for i in range(n_days+1):
    a=np.random.randint(5,10,21)
    b=np.random.randint(5,10,21)
    data_dict['dayN+'+str(i)]=2*(a+b)/2/v/gf #instead of str(i), you could append the key with datetime.now(), etc.

data_dict

{'dayN+0': array([0.275, 0.275, 0.4  , 0.3  , 0.325, 0.425, 0.4  , 0.45 , 0.3  ,
        0.375, 0.375, 0.35 , 0.425, 0.35 , 0.4  , 0.325, 0.3  , 0.3  ,
        0.35 , 0.3  , 0.375]),
 'dayN+1': array([0.3  , 0.275, 0.325, 0.375, 0.4  , 0.425, 0.325, 0.325, 0.4  ,
        0.35 , 0.3  , 0.4  , 0.375, 0.25 , 0.375, 0.375, 0.45 , 0.35 ,
        0.425, 0.35 , 0.4  ]),
 'dayN+2': array([0.4...

The Complete Guide to Do-loop, Do-while and Do-Until , In this example, you would like to calculate what your account balance will be A small “i” is the most commonly used index variable, but any variable name that So, in SAS terms, we will iterate the index variable “i" from 1 to 52 by 2, since we see the updated dataset shown below, the loop now stopped executing after� DECLARE @amountTotal numeric(10, 2)=0; UPDATE dbo.someTable SET --- How much we've added to the amount column: @amountTotal=@amountTotal+0.1*amount, --- .. and perform the actual update of amount: amount=amount*1.1 WHERE someCriteria=1; Other types of aggregates may take more than one variable to compute, but are also possible.


Loop control statements — Object-Oriented Programming in Python , In the condition step, you perform a test on the variable to see whether you should a while loop, make sure that the variable you use in your condition is updated make a big difference if it were an expensive, time-consuming calculation and Or that we want to find and capitalise all the animal names in a list of animal� The While Loop Now let’s tackle the “while” loop. Again, from Programming 101, we all know that in a “while” loop, a condition is evaluated first and if it returns true then the statements inside the “while” loop execute. When the condition returns false, the control comes out of loop and jumps to the next statement after the


1.13. Loops and Sequences — Hands-on Python Tutorial for Python 3, To prepare for that we first follow how variables can be updated in an even statement uses the values of variables when the line starts executing (what is It has the heading starting with for , followed by a variable name ( count in this If we call the number from the list num , the main calculation line in the loop could be. Suppose you want to add the first ten positive integers using the Do While loop in VBA. To do this, you can use the Do While loop until the next number is less than or equal to 10. As soon as the number is greater than 1o, your loop would stop. Here is the VBA code that will run this Do While loop and the show the result in a message box.


How to make dynamic variable names (A1, A2, , An) with "for" loop , Your difficulty in debugging this should be a major hint that you should not be using eval. Meanwhile, build up the character vector and assign it to a variable, and� 1. The while loop . 2. The do-while loop . 3. The for loop While Loop. A while loop is the most straightforward looping structure. The basic format of while loop is as follows: while (condition) { statements; } It is an entry-controlled loop. In while loop, a condition is evaluated before processing a body of the loop.