for each (root of variable) in SPSS - loops

I'm new in SPSS program but I've tried to search a solution to my problem. The idea is find a way to loop through some variable names (or I think so). I want to create the variables P216_XX, P217_XX, T211_XX and OCUPAC_XX, with other variables like some inputs (P216_13, P216_12, P216_11, P216_10, P217_13, P217_12, P217_11, P217_10, T211_13, T211_12, T211_11, T211_10, OCUPAC_13, OCUPAC_12, OCUPAC_11, OCUPAC_10 and IIIDDDEEE).
I'm following the next tedious instruction:
******TEDIOUS INSTRUCTION:
*****P216_XX.
IF (IIIDDDEEE=1) P216_XX=P216_13.
EXECUTE.
IF (IIIDDDEEE=2) P216_XX=P216_12.
EXECUTE.
IF (IIIDDDEEE=3) P216_XX=P216_11.
EXECUTE.
IF (IIIDDDEEE=4) P216_XX=P216_10.
EXECUTE.
*****P217_XX.
IF (IIIDDDEEE=1) P217_XX=P217_13.
EXECUTE.
IF (IIIDDDEEE=2) P217_XX=P217_12.
EXECUTE.
IF (IIIDDDEEE=3) P217_XX=P217_11.
EXECUTE.
IF (IIIDDDEEE=4) P217_XX=P217_10.
EXECUTE.
*****T211_XX.
IF (IIIDDDEEE=1) T211_XX=T211_13.
EXECUTE.
IF (IIIDDDEEE=2) T211_XX=T211_12.
EXECUTE.
IF (IIIDDDEEE=3) T211_XX=T211_11.
EXECUTE.
IF (IIIDDDEEE=4) T211_XX=T211_10.
EXECUTE.
*****OCUPAC_XX.
IF (IIIDDDEEE=1) OCUPAC_XX=OCUPAC_13.
EXECUTE.
IF (IIIDDDEEE=2) OCUPAC_XX=OCUPAC_12.
EXECUTE.
IF (IIIDDDEEE=3) OCUPAC_XX=OCUPAC_11.
EXECUTE.
IF (IIIDDDEEE=4) OCUPAC_XX=OCUPAC_10.
EXECUTE.
In stata, the form would reduce the previous syntax with something like that:
foreach x in P216 P217 T211 OCUPAC {
replace `x'_XX=`x'_13 if IIIDDDEEE==1
replace `x'_XX=`x'_12 if IIIDDDEEE==2
replace `x'_XX=`x'_11 if IIIDDDEEE==3
replace `x'_XX=`x'_10 if IIIDDDEEE==4
}
*
In SPSS, I proved with vector, but SPSS don't accept me root in the vector, only variables. I don't know how to continue and I haven't found something similar yet
Is possible found a solution with native SPSS language? (I forgot to comment that in my work, the "administrator" doesn't allow us to install other programs like Python). What would be a possible solution?

There are a quite a lot of ways you can simplify this code, look up:
DEFINE/ENDDEFINE, SPSS's macro language.
DO IF
DO REPEAT
You need to be familiar with each and know what your end goal is to assess which option is best for you.

Related

Loop over a do file with regression model in Stata

I am estimating several regressions in Stata with the same functional form. I'd like to perform my estimations by looping over a .do file that contains the "program" for my regression. The (simplified) code I have attempted is as follows:
local vars waz haz whz cough fever diar
foreach depvar of local vars {
forvalues i = 10(5)15 {
do "Regression.do"
}
}
Where "Regression.do" is this code:
reg `depvar' distance_`i'
est store `depvar'_`i'
Stata returns an error message: "Error: last estimates not found." How can I amend my code so that I can execute the .do file in a loop?
Simplifying your code as suggested by #Nick Cox is best, however, to answer your question the solution is below:
main do-file
local vars waz haz whz cough fever diar
foreach depvar of local vars {
forvalues i = 10(5)15 {
do "Regression.do" "`i'"
}
}
regression.do
reg `depvar' distance_`1'
est store `depvar'_`1'
The do-file is hardly needed in this example. Using its contents directly avoids the problem that local macros are ... local ... meaning only visible within the same program space.
foreach depvar in waz haz whz cough fever diar {
foreach num in 10 15 {
reg `depvar' distance_`num'
est store `depvar'_`num'
}
}
is one way to simplify your code. although that is partly a matter of taste.
See also https://journals.sagepub.com/doi/pdf/10.1177/1536867X20931028 for more on what bit you.
I see a lot of Stata code which puts content into a local macro only to take it out again just once and immediately. This is like "I have a pen. I put it in a box. I take it out of the box. I now have my pen again." Sometimes the box (meaning, local macro) is a good idea; but other times there is no gain to writing the code that way.
You do flag that you have fuller code, which is fair enough, and at some point having a do-file and passing arguments to it may appear to be better style.
As partly explained in the answer of #24thDan, you can pass arguments to a do-file, which is how a local macro's contents can become visible to and within a do-file.
You can rewrite your do-file this way
* regression.do
args depvar num
regress `depvar' distance_`num'
est store `depvar'_`num'
and within your loops call it with
do regression.do `depvar' `num'
As in the other answer, you can within the do-file refer to the first, second, ... arguments numbering them as 1, 2, and so on, but I recommend the use of args to map those local macros to other macros with intelligible names.

Gremlin: The provided traverser does not map to a value

g.V()
.has('atom', '_value', 'red').fold()
.coalesce(unfold(), addV('atom').property('_value', 'red')).as('atom')
.out('view').has('view', '_name', 'color').fold()
.coalesce(unfold(), addE('view').from('atom').to(addV('view').property('_name', 'color')))
Gives me an error:
The provided traverser does not map to a value: []->[SelectOneStep(last,atom)] (597)
What does it mean?
Adding to this in case someone else comes across this.
This specific error occurs when you use the id as a string in from() instead of the vertex object.
To see what I mean, as a simple test run the following gremlin query:
g.addE('view').from('atom').to(addV('view').property('_name', 'color'))
then run this query:
g.addE('view').from(V('atom')).to(addV('view').property('_name', 'color'))
The first query will give you the error stated above, the second one will not.
So it looks like when as() is followed by fold() it deletes the variable set in the as() step. I used aggregate() instead as follows:
g.V()
.has('atom', '_value', 'red')
.fold().coalesce(
unfold(),
addV('atom').property('_value', 'red')
)
.aggregate('atom')
.out('view').has('view', '_name', 'color')
.fold().coalesce(
unfold(),
addE('view')
.from(select('atom').unfold())
.to(addV('view').property('_name', 'color'))
.inV()
)
The as() step is what is known as a reducing barrier step. With reducing barrier steps any path history of a query (such as applying a label via as()) is lost. In reducing barrier steps many paths are reduced down to a single path. After that step there would be no way to know which of the many original labeled vertices would be the correct one to retrieve.

Appending values to DataSet in Apache Flink

I am currently writing an (simple) analytisis code to sum time connected powerreadings. With the data being assumingly raw (e.g. disturbances from the measuring device have not been calculated out) I have to account for disturbances by calculation the mean of the first one thousand samples. The calculation of the mean itself is not a problem. I only am unsure of how to generate the appropriate DataSet.
For now it looks about like this:
DataSet<Tupel2<long,double>>Gyrotron_1=ECRH.includeFields('11000000000'); // obviously the line to declare the first gyrotron, continues for the next ten lines, assuming separattion of not occupied space
DataSet<Tupel2<long,double>>Gyrotron_2=ECRH.includeFields('10100000000');
DataSet<Tupel2<long,double>>Gyrotron_3=ECRH.includeFields('10010000000');
DataSet<Tupel2<long,double>>Gyrotron_4=ECRH.includeFields('10001000000');
DataSet<Tupel2<long,double>>Gyrotron_5=ECRH.includeFields('10000100000');
DataSet<Tupel2<long,double>>Gyrotron_6=ECRH.includeFields('10000010000');
DataSet<Tupel2<long,double>>Gyrotron_7=ECRH.includeFields('10000001000');
DataSet<Tupel2<long,double>>Gyrotron_8=ECRH.includeFields('10000000100');
DataSet<Tupel2<long,double>>Gyrotron_9=ECRH.includeFields('10000000010');
DataSet<Tupel2<long,double>>Gyrotron_10=ECRH.includeFields('10000000001');
for (int=1,i<=10;i++) {
DataSet<double> offset=Gyroton_'+i+'.groupBy(1).first(1000).sum()/1000;
}
It's the part in the for-loop I'm unsure of. Does anybody know if it is possible to append values to DataSets and if so how?
In case of doubt, I could always put the values into an array but I do not know if that is the wise thing to do.
This code will not work for many reasons. I'd recommend looking into the fundamentals of Java and the basic data structures and also in Flink.
It's really hard to understand what you actually try to achieve but this is the closest that I came up with
String[] codes = { "11000000000", ..., "10000000001" };
DataSet<Tuple2<Long, Double>> result = env.fromElements();
for (final String code : codes) {
DataSet<Tuple2<Long, Double>> codeResult = ECRH.includeFields(code)
.groupBy(1)
.first(1000)
.sum(0)
.map(sum -> new Tuple2<>(sum.f0, sum.f1 / 1000d));
result = codeResult.union(result);
}
result.print();
But please take the time and understand the basics before delving deeper. I also recommend to use an IDE like IntelliJ that would point to at least 6 issues in your code.

How to build a list from a database in prolog?

I'm am trying to build a list that lists all the friends from and to two people in database. The problem I'm running into is that I get stuck in a infinite loop. This is what i have so far.
is_friends(From,To):- friend(From,To).
is_friends(From,To):- friend(From,Z), is_friends(Z,To).
Here is the database i'm working with,
friend(christian,margaret).
friend(christian,jas).
friend(christian,todd).
friend(christian,ji).
friend(christian,geener).
friend(todd,christian).
friend(todd,susan).
friend(susan,todd).
friend(jas,christian).
friend(jas,geener).
friend(jas,clark).
friend(geener,christian).
friend(geener,jas).
friend(geener,ji).
friend(clark,pat).
friend(pat,mike).
friend(pat,clark).
friend(margaret,christian).
friend(ji,christian).
friend(ji,geener).
I guess i'm wondering is there a way for prolog to remember that i already checked a database and can move on to the next one?
This is what my output is supposed to look like
is_friends(From, To).
L = [christian, jas, clark).

Can't use paramaters in HP Loadrunner 11

I have the following code:
lr_output_message( "We are on iteration #%s", lr_eval_string( "{iteration}" ) );
Return log message:
We are on iteration #{iteration}
Did anyone have the same poblem?
A few hours ago, it was works fine.
The only time you'll see an output like that is if LoadRunner can't find a parameter with that precise name.
Usually this is because you've done something wrong; a typo, etc.
Did you define "iteration" EXACTLY like you are using it? Or did you do something like use a capital 'I' (Iteration), or spell it differently, etc?
Please try this:
lr_output_message( "We are on iteration # %d", atoi(lr_eval_string("{iterationnumber}")));

Resources