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.
Related
can someone help me figure out how to select cases containing number 1, for example I coded nausea as side effect as 1 and was then noting it with other side effects as 1234 and now i wan to select all combinations with 1 but i cant figure out how. Or at least how to properly name what I am trying to achieve, since I am fairly new to spss so I can try to further search online.
I have tried variable = 1 and variable = 1 and neither worked and a few random commands that did not work either. I have put the variable as string and did not change anything either.
Once you change the variable into text you can use text search commands to find "1" within the text, like this for example:
compute nausea=(char.index(YourVariable,"1")>0).
char.index command searches for "1" in YourVariable - if it is there, it will output it's position in the text. If it isn't there, the output is 0. So nausea will get a value of 1 in all cases that contain "1" and will gat a value of 0 in all cases that don't.
NOTE - if you get as high as 10 in your numbers, this method will fail, as "10" contains "1". In order for any method to work here, you'd need to add a delimiter between the numbers when you record them, e.g. "1,3,8,17,22".
Our task is create a table, and read values to the table using a loop. Print the values after the process is complete. - Create a table. - Read the number of values to be read to the table. - Read the values to the table using a loop. - Print the values in the table using another loop. for this we had written code as
local table = {}
for value in ipairs(table) do
io.read()
end
for value in ipairs(table) do
print(value)
end
not sure where we went wrong please help us. Our exception is
Input (stdin)
3
11
22
abc
Your Output (stdout)
~ no output ~
Expected Output
11
22
abc
Correct Code is
local table1 = {}
local x = io.read()
for line in io.lines() do
table.insert(table1, line)
end
for K, value in ipairs(table1) do
print(value)
end
Let's walk through this step-by-step.
Create a table.
Though the syntax is correct, table is a reserved pre-defined global name in Lua, and thus cannot should not be declared a variable name to avoid future issues. Instead, you'll need to want to use a different name. If you're insistent on using the word table, you'll have to distinguish it from the function global table. The easiest way to do this is change it to Table, as Lua is a case-sensitive language. Therefore, your table creation should look something like:
local Table = {}
Read values to the table using a loop.
Though Table is now established as a table, your for loop is only iterating through an empty table. It seems your goal is to iterate through the io.read() instead. But io.read() is probably not what you want here, though you can utilize a repeat loop if you wish to use io.read() via table.insert. However, repeat requires a condition that must be met for it to terminate, such as the length of the table reaching a certain amount (in your example, it would be until (#Table == 4)). Since this is a task you are given, I will not provide an example, but allow you to research this method and use it to your advantage.
Print the values after the process is complete.
You are on the right track with your printing loop. However, it must be noted that iterating through a table always returns two results, an index and a value. In your code, you would only return the index number, so your output would simply return:
1
2
3
4
If you are wanting the actual values, you'll need a placeholder for the index. Oftentimes, the placeholder for an unneeded variable in Lua is the underscore (_). Modify your for loop to account for the index, and you should be set.
Try modifying your code with the suggestions I've given and see if you can figure out how to achieve your end result.
Edited:
Thanks, Piglet, for corrections on the insight! I'd forgotten table itself wasn't a function, and wasn't reserved, but still bad form to use it as a variable name whether local or global. At least, it's how I was taught, but your comment is correct!
Can this (Google Sheet) =IFS syntax be improved?
=IFS(and(E42>E38;E42>E34;E42>E30;E42>E26;E42>E22;E42>E18); "Cattleman";
and(E38>E42;E38>E34;E38>E30;E38>E26;E38>E22;E38>E18); "Naturalist";
and(E34>E42;E34>E38;E34>E30;E34>E26;E34>E22;E34>E18); "Farmer";
and(E30>E42;E30>E38;E30>E34;E30>E26;E30>E22;E30>E18); "Carpenter";
and(E26>E42;E26>E38;E26>E30;E26>E34;E26>E22;E26>E18); "Blacksmith";
and(E22>E42;E22>E38;E22>E30;E22>E34;E22>E26;E22>E18); "Miner";
and(E18>E42;E18>E38;E18>E30;E18>E34;E18>E22;E18>E26); "Builder")
And how can I add a default value so that if this syntax returns FALSE it doesn't say #N/A! in the cell, but "No class" or something similar instead (or empty)?
One obvious way would be to replace the ANDs with a MAX. Why? In the first line, if E42 is greater than all of the other cells, it must be greater than the MAX of them. So the condition in this line
E42 > MAX(E38; E34; E30; E26; E22; E18)
which looks much cleaner. Repeat for the other lines.
Trying to simplify it more, the logic of the formula seems to be that depending on which of the cells is the greatest, you choose a particular literal value. There is a function for that! I'd try this (can't test it though without access to your data)
=CHOOSE(
MATCH(
MAX(E42; E38; E34; E30; E26; E22; E18);
{E42; E38; E34; E30; E26; E22; E18});
"Cattleman"; "Naturalist"; "Farmer"; "Carpenter"; "Blacksmith"; "Miner"; "Builder")
wrap it in IFERROR like this:
=IFERROR(IFS(
AND(E42>E38;E42>E34;E42>E30;E42>E26;E42>E22;E42>E18);"Cattleman";
AND(E38>E42;E38>E34;E38>E30;E38>E26;E38>E22;E38>E18);"Naturalist";
AND(E34>E42;E34>E38;E34>E30;E34>E26;E34>E22;E34>E18);"Farmer";
AND(E30>E42;E30>E38;E30>E34;E30>E26;E30>E22;E30>E18);"Carpenter";
AND(E26>E42;E26>E38;E26>E30;E26>E34;E26>E22;E26>E18);"Blacksmith";
AND(E22>E42;E22>E38;E22>E30;E22>E34;E22>E26;E22>E18);"Miner";
AND(E18>E42;E18>E38;E18>E30;E18>E34;E18>E22;E18>E26);"Builder");
"No class")
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.
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.