I have two variables, A and B, that both use a 5-digit code starting with 50. So variable A might be 50123 to designate orange and B might be 50123 to designate apple. The codes were originally constructed to be concatenated together to create a single hierarchy code. I now need to consider them independently and must be able to distinguish between a code for variable A and variable B.
Bottom line is I want to recode variable A items from 50123 to 60123 and variable B items from 50123 to 70123. What's the best way to do this in SPSS without having to manually enter items either in syntax or the recode dialog box?
Thanks
If variables A and B are numbers, you could try
compute A=A+10000.
compute B=B+20000.
If they are strings, this should work:
compute A=concat("6", char.substr(A,2,5)).
compute B=concat("7", char.substr(B,2,5)).
Related
I'd like to create a new variable based on values of other variables (which is absolutely simple) BUT I encounter the problem when I need to find what function I should use when it gets to make specific condition.
The problem is I need an output 0-1 (1 when the condition is met & 0 when it's not) but not all the variables need to met this condition but at least 6 from all of 9.
In following example the condition is that the output will be 1 when all variables met condition and I need to specify that at least 6 from them need to do so:
Q25=3 & Q39=2 & Q38=1 & Q37=3 & Q36=2 &
Q35=1 & Q34=1 & Q33=3 & Q32=2
I tried to find solutions in group of functions but didn't find the specific one. Hope I wrote it clear enough to understand my problem.
Instead of using a logical condition, you can turn this into a mathematical condition. SPSS treats each logical value as a mathematical 0-1 value too, so:
compute YourNewVar=sum(Q25=3, Q39=2, Q38=1, Q37=3, Q36=2,
Q35=1, Q34=1, Q33=3, Q32=2)>=6.
So...I want to create five different polynomials inside a loop in order to make a Sturm sequence, but I don't seem to be able to dynamically name a set of polynomials with different names.
For example:
In the first iteration it would define p1(x):whatever
Then, in the second iteration it would define p2(x):whatever
Lastly, in the Nth iteration it would define pn(x):whatever
So far, I have managed to simply store them in a list and call them one by one by its position. But surely there is a more professional way to accomplish this?
Sorry for the non-technical language :)
I think a subscripted variable is appropriate here. Something like:
for k:1 thru 5 do
p[k] : make_my_polynomial(k);
Then p[1], ..., p[5] are your polynomials.
When you assign to a subscripted variable e.g. something like foo[bar]: baz, where foo hasn't been defined as a list or array already, Maxima creates what it calls an "undeclared array", which is just a lookup table.
EDIT: You can refer to subscripted variables without assigning them any values. E.g. instead of x^2 - 3*x + 1 you could write u[i]^2 - 3*u[i] + 1 where u[i] is not yet assigned any value. Many (most?) functions treat subscripted variables the same as non-subscripted ones, e.g. diff(..., u[i]) to differentiate w.r.t. u[i].
Being a novel on SPSS I am struggling with finding duplicate cases based on a string-variable in a dataset containing approx 33,000 cases.
I have a variable named "nr" that is supposed to be unique id for every case. However, it turns out that some cases might have two different values in "nr" entered,the only difference being the last character. Resulting in a case being shown as two separate rows.
The structure of the var "nr" is a as follows: XX-XXXXXXX-X or X-XXXXXXX-X i.e 2-7-1 characters or 1-7-1 characters.
I would like to sort out all cases that have a "nr" equal to another case except for the last character.
To illustrate, with a succesfull syntax I would hopefully be able to sort cases like these out from the whole dataset:
20-4026988-2
20-4026988-3
5-4026992-5
5-4026992-8
20-4027281-2
20-4027281-3
Anyone have an idea on how to make a syntax for this? Would be so grateful for any input!
I suggest to create a new variable without that last character, and then look for the doubles:
* first creating some sample data to play with.
data list list/ID (a15).
begin data.
20-4026988-2
12-2345678-7
20-4026988-3
5-4026992-5
5-4026992-8
12-1234567-1
20-4027281-2
6-1234567-1
20-4027281-3
end data.
* now creating the new variable and counting the occurrences of each shortened ID.
string ShortID (a15).
compute ShortID=char.substr(ID,1,char.rindex(ID,"-")).
* also possible: compute ShortID=char.substr(ID,1,char.length(rtrim(ID))-1).
aggregate out=* mode=add /break=ShortID/occurrences=n.
* at this point you can filter based on the number or `occurrences` or sort them.
sort cases by occurrences (d) ShortID.
After removing the last character, you can use Data > Identify Duplicate Cases to find the dups. It as a number of useful options for this.
Is it possible in Maxima for me to declare a matrix with a given number of rows and of columns without initializing it? The closest thing I found is the function zeromatrix. And how to declare vectors like that?
I want to have them declared so I can fill them in loops.
Thank you.
You don't need to declare things in Maxima prior to using them.
For instance, you could create some lists containing the rows, as in
for i:1 thru 6 do myrow[i]:makelist(i+j,j,1,5);
Then you could construct the matrix with
M[i,j]:=myrow[i][j]
and
genmatrix(M,6,5)
Of course, this can be made more precise if you provide more details of what you are trying to do...
I am not used to SPSS so this question will sound stupid:
I need to change fragments of a cell in spss, exemple:
'1.28'
'2.69'
'3.57'
to
'a.28'
'b.69'
'c.57'
What's the best way to do it?
Tks.
This is assuming the variable you want to recode is called 'VarA', and that it is numeric.
This creates a copy of the variable, converts it to a string, and then uses those values to create a new version that is recoded.
RECODE VarA (ELSE = COPY) INTO VarA_String.
ALTER TYPE VarA_String(A8).
EXECUTE.
COMPUTE VarA_r=REPLACE(VarA_String,'1.','a.').
COMPUTE VarA_r=REPLACE(VarA_String,'2.','b.').
COMPUTE VarA_r=REPLACE(VarA_String,'3.','c.').
EXECUTE.
The syntax is a little different in SPSS Modeler and bear with me as I can only attach one image until I have a certain reputation on SO.
After you convert VarA into a string (which I called to_str) you can use the replace command to change part of the substring, ie:
to_string(VarA)
for the first Derive node, and:
replace('1.','a.',to_str)
for the second Derive node, this command replaces all occurrences of SUBSTRING1 with SUBSTRING2 in STRING and you will get the same result but in Modeler, see the sample stream here
Assuming that these are strings, see the replace function in COMPUTE. If there are just a few, though, just edit the cells in the Data Editor.