Using char.sub command on multiple variables - spss

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.

Related

how to select all cases containing a specific number in spss

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".

Lua string.match captures in a single variable?

i want to remove separation characters from inbetween numbers in lua. For example: 19,300 -> 19300.
I am able to extract the two "parts" of the number using
a,b = string.match(amount, '(%d+),*(%d*)')
but have to store the results in two separate variables first. I would like to have a clean way of storing all capture groups as a concatinated, single variable, instead of having to do c=a..b in an extra step. Is there a way to achieve this?
You can simply remove all non-digits: local stripped = string.gsub( amount, '%D', '' ).

We giving a task for Lua table but it is not working as expectable

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!

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.

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

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.

Resources