Increment specific column value in a block dataset and loop several times

for loop sas
r repeat function for each column
use do loop to compute the value of a new variable
r loop through multiple data frames
sas do loop to create datasets
for loop in r
which of the following functions lists all columns in a dataset with few values from each column
iterative do loops

I am trying to generate a huge dataset in the mentioned order.

4 0 1 642 643
4 642 643 1283 12
4 1283 1284 1924 1925
4 1924 1925 2565 2566
4 2565 2566 3206 3207
4 3206 3207 3847 3848
4 3847 3848 4488 4489
4 4488 4489 5129 5130

4 1 2 643 644
4 643 644 1284 1285
4 1284 1285 1925 1926
4 1925 1926 2566 2567
4 2566 2567 3207 3208
4 3207 3208 3848 3849
4 3848 3849 4489 4490
4 4489 4490 5130 5131

4 2 3 644 645
4 644 645 1285 1286
4 1285 1286 1926 1927
4 1926 1927 2567 2568
4 2567 2568 3208 3209
4 3208 3209 3849 3850
4 3849 3850 4490 4491
4 4490 4491 5131 5132

Description: There are 5 columns with the first column having a constant '4'. There are 8 rows in a block (data separated by a blank line).

Now, my idea is to copy the previous block (starting with lines 1-8) to the lines 10-17 and increment the number in columns 2-5 by one. This process (now lines 10-17 have to be copied in pasted in lines 19-27 and columns 2 -5 have to be incremented by 1) has to be iterated around 640 times to generate my required set.

Can anyone suggest me how to generate this type of dataset?

$ cat tst.awk
{
    for (fldNr=1; fldNr<=NF; fldNr++) {
        flds[NR,fldNr] = $fldNr
    }
}
END {
    maxRecs = 3
    for (recNr=1; recNr<=maxRecs; recNr++) {
        for (lineNr=1; lineNr<=NR; lineNr++) {
            printf "%s", flds[lineNr,1]
            for (fldNr=2; fldNr<=NF; fldNr++) {
                printf " %s", flds[lineNr,fldNr]+(recNr-1)
            }
            print ""
        }
        print ""
    }
}

.

$ cat file
4 0 1 642 643
4 642 643 1283 12
4 1283 1284 1924 1925
4 1924 1925 2565 2566
4 2565 2566 3206 3207
4 3206 3207 3847 3848
4 3847 3848 4488 4489
4 4488 4489 5129 5130

.

$ awk -f tst.awk file
4 0 1 642 643
4 642 643 1283 12
4 1283 1284 1924 1925
4 1924 1925 2565 2566
4 2565 2566 3206 3207
4 3206 3207 3847 3848
4 3847 3848 4488 4489
4 4488 4489 5129 5130

4 1 2 643 644
4 643 644 1284 13
4 1284 1285 1925 1926
4 1925 1926 2566 2567
4 2566 2567 3207 3208
4 3207 3208 3848 3849
4 3848 3849 4489 4490
4 4489 4490 5130 5131

4 2 3 644 645
4 644 645 1285 14
4 1285 1286 1926 1927
4 1926 1927 2567 2568
4 2567 2568 3208 3209
4 3208 3209 3849 3850
4 3849 3850 4490 4491
4 4490 4491 5131 5132

Just change maxRecs=3 to maxRecs=640 or whatever value you like.

[PDF] Using DataSets to Define Macro Loops and Local , Often, the number of times to execute the macro or the parameter values to use each row (observation) is one loop iteration and each column (variable) is one version of this method used in a data block in order to export a dataset to a Line 3 initializes the loop counter &now to zero and line 9 uses %eval to increment� 0 Increment specific column value in a block dataset and loop several times Feb 23 '19 0 Writing a sequence of numbers like: 1 22 333 4444 55555 Aug 2 '14 0 Python Error: “ImportError: No module named six” Sep 5 '14

This might work for you (GNU sed & bash):

cat <<\!>file
4 0 1 642 643
4 642 643 1283 12
4 1283 1284 1924 1925
4 1924 1925 2565 2566
4 2565 2566 3206 3207
4 3206 3207 3847 3848
4 3847 3848 4488 4489
4 4488 4489 5129 5130

!

for n in {0..640}; do sed 's/\S\+/$((&+'$n'))/2g;s/.\+/printf "%d %d %d %d %d" &/e' file;done

On retrospect this will work too:

for n in {0..640}; do sed 's/\S\+/$((&+'$n'))/2g;s/.\+/echo "&"/e' file;done  

18.1 - Constructing Do Loops, After SAS has added 3 to the answer variable four times, SAS exits the DO to notice about the DATA step is that there is no input data set or input data file. the value of the index variable at which you want SAS to stop the loop; increment is In this case, we are not interested in one particular multiplication, but rather in a� We set it to a value equal to start_counter at the beginning of the loop and then increment it by 1 during each loop till it meets the end condition. The loop will execute till the time the value of the counter becomes equal to end_counter. The loop executes for the last time when both the above values match and then the loop stops.

Using Perl one-liner

 perl -0777 -ne ' while( $i++<3) { s/(?!^)(\d+)/$1+1/gme; print "$_\n" } '

with the given inputs

$ cat saideep.txt
4 0 1 642 643
4 642 643 1283 12
4 1283 1284 1924 1925
4 1924 1925 2565 2566
4 2565 2566 3206 3207
4 3206 3207 3847 3848
4 3847 3848 4488 4489
4 4488 4489 5129 5130

$ perl -0777 -ne ' while( $i++<3) { s/(?!^)(\d+)/$1+1/gme; print "$_\n" } ' saideep.txt
4 1 2 643 644
4 643 644 1284 13
4 1284 1285 1925 1926
4 1925 1926 2566 2567
4 2566 2567 3207 3208
4 3207 3208 3848 3849
4 3848 3849 4489 4490
4 4489 4490 5130 5131

4 2 3 644 645
4 644 645 1285 14
4 1285 1286 1926 1927
4 1926 1927 2567 2568
4 2567 2568 3208 3209
4 3208 3209 3849 3850
4 3849 3850 4490 4491
4 4490 4491 5131 5132

4 3 4 645 646
4 645 646 1286 15
4 1286 1287 1927 1928
4 1927 1928 2568 2569
4 2568 2569 3209 3210
4 3209 3210 3850 3851
4 3850 3851 4491 4492
4 4491 4492 5132 5133


$

Repeating things: looping and the apply family, Repeating yourself will cost you time, both now and later, and potentially Then you then Split it up into many smaller datasets, Apply a function to Moreover, they are the building block for other data structures, like data.frame and matrix . loads the data, and tests it against some hypothesised value H0. In VBA, you can loop through a range of cells, applying actions to each cell in the range. If you want to test a condition for each cell in a range using VBA, the best way is to loop through the range, testing each cell. Here are two code examples to demonstrate how to loop through a range.

Optimum approach for iterating over a DataFrame, We are going to use the Titanic dataset that was used in the previous post. % timeit test_iterrow(titanic_df)Output: 10 loops, best of 3: 73.1 ms per loop That is more than 7 times faster than using iterrows. statements effecting the values across many columns of a single row, apply() will not be as useful. $\begingroup$ a function that takes the columns of a dataframe that I give as an input and maps the new values onto old values,just in those columns ,is what I'm trying to figure out ,without using loops .we can do something like it with "Purrr" package,but not sure how to . $\endgroup$ – ultron Nov 18 '16 at 15:02

The following bash script does the required as mentioned in the question.

#!/bin/bash
for k in {1..639}
do
 echo "$k th iteration"
 tail -8 MWE.txt > i
 awk '{print $1, $2+1, $3+1, $4+1, $5+1}' i > j
 cat j >> MWE.txt
done

Note here I increment the loop counter variable 'k' until 639 as I provide the first set of data (initial 8 rows of data).

Example 3: How to Increment a Macro DO Loop , Example 4: How to Use Character Values on a Macro %DO Loop Tree level 3. Node 4 of 17. Example 5: Place All SAS Data Set Variables into� This data set includes 3,023 rows of data and 31 columns. While 31 columns is not a tremendous number of columns, it is a useful example to illustrate the concepts you might apply to data with many more columns. If you want to follow along, you can view the notebook or pull it directly from github. Let’s get started by reading in the data.

A Tutorial on Loops in R - Usage and Alternatives, Some loops execute for a prescribed number of times, as controlled by a counter or an index, incremented at each iteration cycle. If the condition is verified, an instruction -or block of instructions- i1 is executed. a bi-dimensional array by setting its elements to specific values. and 30 columns). When you use ‘Step 2’, it tells the code to increment the ‘Count’ value by 2 every time the loop is run. So the Count value starts from 2 and then becomes 4, 6, 8 and 10 as the looping occurs. NOTE: Another way of doing this could be to run the loop from 1 to 10 and within the loop check whether the number is even or odd.

Loops in R, Some loops execute for a prescribed number of times, as controlled by a counter or an index, incremented at each iteration cycle. Create a vector filled with random normal values u1 <- rnorm(30) print("This loop calculates the square of exhange and want to explore and analyse trends in certain stocks. for index = values, statements, end executes a group of statements in a loop for a specified number of times. values has one of the following forms: initVal : endVal — Increment the index variable from initVal to endVal by 1 , and repeat execution of statements until index is greater than endVal .

Master the art of looping in JavaScript with these incredible tricks, It is very handy to execute a block of code a number of times. It uses a The counter is increased by a specific value every time the loop runs. Inside this row I am adding the 3 columns — 0th index, 1st index, and 2nd index. We have some missing values in our dataset. These are mostly in the Item_Weight and Outlet_Size. I will handle the missing values for Outlet_Size right now but we’ll handle the missing values for Item_Weight later in the article using the GroupBy function!

Comments
  • I see that I can use 'tail' to capture the last 8 lines in the file and then use 'awk' to do the incremental part but then I will have 640 files to append which is not an elegant way. ex: $1 tail -8 MWE.txt > MWE1.txt || $2 awk '{print $1, $2+1, $3+1, $4+1, $5+1}' MWE1.txt > op1
  • It's vi, not bash. ^[ is escape, ^M is return.
  • @jhnc I tried the command in vi but it does not work. Does it work in visual mode or any specific conditions? And also is there a syntax error as the number of brackets don't match!!
  • I open a file in vi, pasted in the data you provided, then typed in the command I gave (the apparent linebreak after the doublequote is a space). Works on nvi and freebsd variants of vi. No idea if vim handles it correctly.
  • One way to get a desired number of iterations: for my $i (1..N)
  • @zdim.. yes thank you!.. btw, I think I have improved on formatting now :-)
  • I don't understand when shall Ctrl+V (Visual mode), CR CR be executed. From my knowledge, a macro is defined in visual mode. If I follow the order you mentioned I get 640 prints of z in insert mode (due to 640iz).
  • Ctrl-V is not visual mode while you are defining a macro with :map. It means "quote next character" in this context. You can test if the macro was defined correctly by typing :map<kbd>CR</kbd> and checking that there is a definition for z. It looks like vim doesn't do the right thing if you try to paste the command sequence, so type it in manually.
  • I tried mapping. But it is not doing the required. Instead of 640 iterations, I tried only doing 2 and it resulted in repeated results (whereas the solution shall be as posted in the question).
  • What do you mean by "repeated results"?
  • After running 2 iterations, I have output as 4 2 3 644 645 || 4 644 645 1285 1286 || 4 1285 1286 1926 1927 || 4 1926 1927 2567 2568 || 4 2567 2568 3208 3209 || 4 3208 3209 3849 3850 || 4 3849 3850 4490 4491 || 4 4490 4491 5131 5132 || and this data repeats 4 times erasing the previous data.