LOOP or DO REPEAT in SPSS - loops

I cannot figure out how to write a LOOP or DO REPEAT for this piece of written syntax. I would like to perform the following set of syntax for about 75 variables - where x would be the variable, any help would be appreciated. Thank you!
DATASET ACTIVATE aabb.
DATASET COPY Tab2.
DATASET ACTIVATE Tab2.
MATCH FILES
/FILE *
/KEEP a b c d x.
DATASET ACTIVATE Tab2.
SELECT IF NOT (x="").
EXECUTE.
SORT CASES BY a b c d.
DATASET ACTIVATE aabb.
SORT CASES BY a b c d.
DATASET ACTIVATE Tab2.
RENAME VARIABLES (x=x1).
SORT CASES BY a b c d.
DATASET ACTIVATE Tab2.
SORT CASES BY a b c d.
DATASET ACTIVATE aabb.
MATCH FILES /FILE=*
/TABLE='Tab2'
/BY a b c d.
EXECUTE.
DELETE VARIABLES x.
DATASET CLOSE Tab2.

Related

IF Formula to serve multiple scenarios

I`m looking for a formula to serve the followings
A, B, C & D are dates
if C falls between A & B then "ok",
if not then "OK-Overdue",
if C blank and D>B then "under-review"
if C blank and D<B then "Pending"
D will be always Today()
It was a bit unclear I must admit, the formula's result will be in another cell, let's say E.
I am assuming that A is always smaller than B. As in, A is start date, B is end date.
=IF(AND(A1<C1;C1<B1);"OK";"OK-Overdue")
This will work, but not when C is blank. Let's check that first:
=IF(ISBLANK(C1);"BLANK";IF(AND(A1<C1;C1<B1);"OK";"OK-Overdue"))
Okay, and we didn't want to print "BLANK" when it's blank, we want to make further checks. Let's make the
if C blank and D>B then "under-review" if C blank and D<B then "Pending"
check first, then we just replace "BLANK" in the formula we have done so far. Okay, let's do the D/B-checks:
=IF(B1<D1;"under-review";"Pending")
Okay, finally let's put that in the "BLANK"-space, and here it is...
Final Result:
=IF(ISBLANK(C1);IF(B1<D1;"under-review";"Pending");IF(AND(A1<C1;C1<B1);"OK";"OK-Overdue"))
Hope it works like you want it to, just let me know if something's off. NOTE: I don't check if D=B. So if D=B, it will say "Pending". :)

Stack multiple columns into one without ignore blank cells

For a stack of several columns and filtering the blank cells, #player0 told me to do it as follows:
https://stackoverflow.com/a/60028660/11462274
But I came across the need for the blank cells not to be ignored, because I'm going to use a script on four different spreadsheets, and each one of them will send data to the first blank row of the columns, so it may happen that some columns have more data than the others and as I am going to record values manually in the column on the side, if I change the positions it will mess up what I record manually.
:
A B C D
E F G H
J K L
N
Expected result:
A
B
C
D
E
F
G
H
J
K
L
N
These blank spaces will be necessary so that when new data is recorded in the columns, the old ones do not change position, remain in the same place where they are.
Would it be possible to do that?
Note: I have already looked in some places saying that if I do this,
the spreadsheet would be immense because of the number of blank lines
that it would take into account. I imagine that it would be necessary
only the blank cells until the last line that has data at the moment,
the rest would not be necessary.
But I was unable to adjust to make this possible.
My solution is so simple it hurts, but you can use =FLATTEN(A1:D)
Yes, that easy.
Apparently, Google never documented this FLATTEN function, but it works PERFECTLY for what you want and what I've needed as well.
Hope someone finds this even though it's been answered, I know I've needed it forever haha.
You want to achieve the following situation.
From
A B C D
E F G H
J K L
N
To
A
B
C
D
E
F
G
H
J
K
L
N
In above case, you want to use only the values from A to N. You don't want to include the empty values after the value of N.
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
Modified formula:
=TRANSPOSE(SPLIT(REGEXREPLACE(TEXTJOIN(",",FALSE,A1:D,"#"),",+#",","),",",,FALSE))
Join the values of "A1:D" using TEXTJOIN().
Remove the empty values after the last character using REGEXREPLACE().
Split the value using SPLIT() from a string to an array.
Transpose the array using TRANSPOSE().
Result:
References:
TEXTJOIN
REGEXREPLACE
SPLIT
TRANSPOSE
If I misunderstood your question and this was not the result you want, I apologize.
you can build on it like:
=ARRAYFORMULA(ARRAY_CONSTRAIN(TRANSPOSE(SPLIT(QUERY(TRANSPOSE(QUERY(TRANSPOSE(
IF(A:D="", CHAR(9), A:D)),,9^9)),,9^9), " ")), ROWS(A:A), 1))

MATLAB: Check if a row exists or not in an array and if not add it to the end of another array

I have two arrays Q and Q2, double arrays 5520x4, both of them are exactly the same at the beginning. Schematically Q is like this:
The rows in Q represent mathematic relations between variables, for example the first row in the Q means:
x0-x3 = 84 and so on.
Also, Q2 is pretty much identical, like this:
During my algorithm, Q has to do some deletions of rows, while Q2 is made to maintain all the rows.
The rows are just mathematic relations that need to be checked from another function, which i am using later in the program.
So, what i really would like to know is how could i check if a specific row exists or not in Q2 after some row deletions from array Q? If it does not exist, it must be added at the end of Q (fifo policy queue). Otherwise, if it does exist, we have nothing to do, the algorithm goes on until the Q is empty.
Could someone please suggest a reliable and efficient way to achieve this check?
I used to have both Q and Q2 as cell arrays, but cellfun was way too slow for more complicated problems.
Any help would be really appreciated.
Thanks a lot for your time,
NickH

How to Write multiple Arrays in excel using Matlab

I have list of values save in variables like. A= 1,2,3,4,5,6,7 and B = 4,5,6,73,2,3,2 //This may be Array or column. Easy one will be preffered
Note A and B values will be dynamic which will be get through some function.
I want to save A's and B's Values in Excel Sheet, like A's values in First column and B's values in 2nd Column.
I read about xlswrite but did not find any scenerio as i require.
And when They are saved then Again want to read them, and to save them in some other variables like C and D. I want to save Data of first column in C and second column data in D.
I read about C = xlsread('filename') but problem with it is. This save all the values in one variable. like if Excell have two columns. Those both will be saved in one variable C. But my requirment is one colum in one varibale and so on.
Take a look at the Documentation
Syntax for writing to excel file
xlswrite(filename,A,sheet,xlRange)
Code:
%// As your variable is a row vector, it is transposed to column vector
%// before writing, as you preferred
xlswrite('outputFileName.xlsx',[A.',B.'],1,'D2')
Note: Make sure that the excel file is closed while writing
Syntax for reading from excel file
num = xlsread(filename,sheet,xlRange)
Code:
%// reading the range into one temporary variable
temp = xlsread('outputFileName.xlsx',1,'D2:E7')
C = temp(:,1)
D = temp(:,2)
I know English is not your primary language, but please try to be as gramatically correct as you can, it will make your questions easier to understand, and easier to answer to.
As for your question, I think you want to use the additional parameter in the functions you found to specify the columns you are writing to:
xlswrite('sheet.xls', A, 'E3')
will write the data of A on the cells E3, ..., E9.
And when you want to retrieve it, you do the same:
C = xlsread('sheet.xls', 'E3-E9')
This will read E3 to E9 cells and put the value in C.

Creating a vector with unique observations from a variable in Stata

What I am mainly trying to do is to create a variable in which I can assign, within a stratum of my sample (defined by an 'id' variable, for instance), a name that is associated with the highest frequency (in the stratum) of this same name in another (string) variable. If tabulate* would work the way I need it to work, my code would run like this:
gen new_class_within_id=""
forvarlues i=1/80 {
tab class_var, matcell(x) if id==`i'
svmat x
sum x2
local name =x1 if x2==r(max)
replace new_class_within_id=`name' if id==`i'
}
That would be the general idea if tabulate would permit storing the unique observation names in a matrix -- the code might have some unintended errors too, of course. But while it does not seem to be possible using the above code, I thought that I could use mkmat if I would be able to store, in the loop, the unique observations inside a vector with some additional coding. Would that be possible? Also, is there an easier way to perform what I want to do?
*Firstly, I thought that using tabulate and extracting the results into a matrix would do the work that I need, but tabulate does not allow me to extract the names of the observations, just the frequencies. tabulate seemed nice because in its output it shows the unique observations of a variable in a column, but I could not find a way to extract those observations the way the output shows.
I think I understand your question, but maybe I don't. Some code:
clear
set more off
input ///
id str1 anothvar
1 a
1 a
1 a
1 b
1 m
2 c
2 c
2 m
2 a
2 z
end
list, sepby(id)
*-----
bysort id anothvar : gen count = _N
bysort id (count): gen newvar = anothvar[_N]
list, sepby(id)
More work needs to be done if you have missings and/or ties.

Resources