SPSS how to turn variables with specific answers and their values into a new variable - spss

I'd like to know how to turn different variables with specific values into a new category variable to extend the specific values? I tried it with compute a variable to get a new one but it didn't work:
this is what I tried but it didn't work:
DO IF ((Q103_01=>3) AND (Q103_05=>3) AND (Q201_06=<2) AND (Q201_07=<2) AND (Q201_08=<2) AND (Q301_06=<2) AND (Q301_07=<2) AND (Q301_08=<2) AND (Q305_04=2) AND (Q305_06=2) AND (Q409_05=1) AND (Q407_07=<2) AND (Q408_04=1) AND (Q408_05=1) AND (Q411_02=<2) AND (Q203_04=<2) AND (Q203_06=<2) AND (Q203_07=<2) AND (Q203_09=<2) AND (Q203_10=<2)).
COMPUTE NewVariable = 1.
END IF.
EXECUTE.
Thanks for help in advance!

When using "Less than or equal to" or "Greater than or equal to" the sequence should be <= (or LE), >= (or GE).
The error message came from using => instead of >=.
Otherwise your syntax should work fine.
But it can be written more efficiently this way:
if cond1 and cond2 and cond3 newvar=1.
without using do if.

Related

ruby on rails pass a value to .last()

I'm pretty new to ruby on rails, so I'm probably missing some syntax. Big picture I am trying to get the value for a specified percentile. Conceptually I am taking my table 'Scores', sorting it, getting the last 'x' values, and then taking the first value. I can't seem to figure out how to pass 'x', which is based on the length of the dataset to the chain.
def get_percentile()
record_count = Scores.count(:id)*0.05
record_threshold = record_count.round()
Score_percentile = Scores.order(:points).last(record_threshold).first().points
return Score_percentile
end
get_percentile
If I just enter .last(20) this works as I expect, so I just don't know how to pass the variable.
Thanks.
You may be passing a 0 into your .last() function with your rounding.
There are a variety of options to make sure you pass at least a 1
[record_threshold, 1].max will give you at least 1. https://apidock.com/ruby/Enumerable/max
Changing .round() to .ceil() rounds up in all instances. https://apidock.com/ruby/Float/ceil

Replacing empty cells in a variable with values from another

I have a dataset with a number of columns. Two of them are practically the same however in variable column 1 there are string data that I would like to extract and replace in empty cells of variable column 2.
I tried using the syntax
If
variable_2 = "".
Compute variable_1 = variable_2.
End If
But do not get anything. Please, could someone help with this?
Much appreciated.
This should be either
if var2="" var2=var1.
(no period after the condition, no "end if")
OR
do if var2="".
compute var2=var1.
end if.
(this is a "do if" and not just an "if" - enables you to add commands after the condition, and not needed here).
In any case, if variable_2 is empty you want to run variable_2=variable_1 and not the reverse.

Using char.sub command on multiple variables

I have to select group of cases starting with specific numbers in multiple variables.
I am using this
CHAR.SUBSTR(variable1,1,x) ="y" | CHAR.SUBSTR(variable2,1,x) ="y" .............| CHAR.SUBSTR(variable40,1,x) ="y".
(x is number of character,y is characters I am choosing) the variables are named similar with just the number 1 to 40 being different
it works but problem is there are 40 variables and code is very length.
any elegant way to write it? like variable1 THRU variable 40?
You can loop through the variables and then select. Like this:
do repeat vr=variable1 to variable40.
if CHAR.SUBSTR(vr,1,1)="y" keep_this=1.
end repeat.
select if keep_this=1.
after running the loop, if any of the variables starts with "y" then the line will be marked with 1 in the variable keep_this. Now you can select only cases where keep_this=1.

Print ResponseIDs of missing values SPSS

I'm creating a variable that will hold missing values from a specific variable. Currently, this works but it gives the missing a value a 1. How do I tell spss to print the respondent's ResponseID instead?
My code below:
COMPUTE Q_2_MIS = MISSING(Q_2).
EXECUTE.
Thanks
Your code returns value of 1 because the condition missing(q_2) is evaluated to TRUE.
Try this:
DO IF MISSING(Q_2).
COMPUTE Q_2_MIS = ResponseID .
END IF.
EXECUTE.
or (as per eli-k's comment) simply use IF:
IF MISSING(Q_2) Q_2_MIS = ResponseID .
EXECUTE.
Note that you might need to create the Q_2_MIS variable first, if you do not have it in your dataset.
Alternatively, if you want to print out the IDs of the respondents with missing in Q_2:
TEMPORARY.
SELECT IF missing(q_2).
LIST ResponseID q_2.
You will see a list of IDs in the SPSS Output, with a (blank) Q_2 next to each ID.

how to use dynamic variable for symbols in ruby where statements

I dont how to accomplish this problem.
I faced with this problem 3 times and each time I put it in my todo list but even tho I tried to find a solution I couldnt.
For examples,
I m trying to create a query with dynamic variables of this example;
User.search(first_name_start: 'K')
there are 3 arguments in this example;
1)first_name - My model attribute
2)start - Query type (start/end/cont )
3)'3' - value
I was able to create dynamic ActiveRecord using static symbols but how am I suppose to make dynamic input
Thanks in advance
EDIT: ADDITIONAL INFORMATION
let me show you a some kind of pseudo-code
varArray.each_with_index |x,index|
queryString=varArray[i]+"_"+filterArray=[i] #lets say varArray[i], this will be first_name, an actual model attribute/a column in my db
#and filterArray like /start/end/with a filter type
#and finally valArray a string value like 'geo' or 'paul'
User.where(queryString valArray[i]).result
I tried to use send(variable) but that didnt help me either, so i dont how should i proceed,
This is one of a few cases where new fancy Ruby 1.9 syntax for defining hashes doesn't cut it. You have to use the traditional hashrocket (=>) that allows you to specify not only symbols, but any arbitrary values as hash keys:
column = "#{first_name}_size_#{query_type}".to_sym
User.where( column => value )
AFAIK, ActiveRecord is able to accept strings instead of symbols as column names, so you don't even need to call to_sym.

Resources