How to repeat a case value until a different value is encountered? - spss

I currently have a data file that is structured like this:
1
-
-
2
-
3
-
I would like it to look like this:
1
1
1
2
2
3
3
Unfortunately I do not how to achieve this in SPSS. Is there are a simple command that could recode the data this way?

I have found the answer, by using the LAG function. (I defined 9999 as a missing value).
IF (variable = 9999) variable=LAG(variable).
EXECUTE.

Related

Modulo alternative Lua

I don't have much coding experience so I don't really know of an efficient alternative to modulo, the issue I have is that I want to have the same funcionality but witouth it ever returning zero if that makes sense.
So I have an arbritary value % 8 and I want my results to go (1,2,3,4,5,6,7,8,1,2,3,etc)
any help or push in the right direction would be appreciated.
I assume you're trying to make indices from 1 to 8 loop. For zero-based offsets from 0 to 7 this would be trivial by using i % 8; consider simply making your table zero-based.
For one-based indices, the simplest way to go is to first subtract 1 to make it zero-based, then apply the modulo to wrap around, then add 1 to make it one-based again: ((i - 1) % 8) + 1.
So I have an arbritary value % 8 and I want my results to go
(1,2,3,4,5,6,7,8,1,2,3,etc)
local result = value % 8 + 1
This is a simple maths problem. If one arrithmetic operator doesn't give you the desired result, use or add others to your formula.

Combine variables in order to create new one

I need to have a new variable ethnicity.
The variables that I have now:
Dutch (if yes = 1, if no = 0)
Russian (if yes = 2, if no =0)
So it looks like that now:
Russian Dutch
2 0
0 1
0 1
2 0
How can I combine "Dutch"and "Russian"variables into new one Ethnicity"?
I want to have this result:
Ethnicity
2
1
1
2
I have tried to it with compute, but it was not successful.
The simple\basic\generic approach is:
if dutch=1 ethnicity=1.
if russian=2 ethnicity=2.
But if I understand the structure of your data right, this should also work:
compute ethnicity=sum(dutch, russian).

How to copy from one variable to another on SPSS

I have a data set with two parent/carer respondents (main and partner) for each participant (child). For one of the variables, only one respondent has given an answer - usually the main respondent, but in some cases it was the partner respondent. I therefore need to fill in some missing main respondent data with data from the partner respondent.
My data looks roughly like this:
MAIN PARTNER I would like the final var as below:
2 -1 2
1 -1 1
-1 2 2
1 . 1
-9 2 2
-8 1 1
2 . 2
1 . 1
etc.
(-1, -8 and -9 are missing values)
All variables are numeric. Where a response is missing from the main respondent, I would like to fill it in from the partner. I cannot seem to get the DO IF/RECODE commands to work.
Any advice on how to do this in SPSS would be hugely appreciated!
More than one way to skin a cat. Depending on your taste, you might create your final variable responder like so:
MISSING VALUES main (-1,-8,-9) .
IF (MISSING(main)) responder=partner .
IF (NOT(MISSING(main))) responder=main .
EXE .
First assign your missing values. Then assign a value to responder based on whether main is missing. Note that MISSING(main) will evaluate true when main has a specified missing value (in this case: -1, -8, or -9) or a system missing value.

AWK (or similar) - change 2 lines below the matching pattern

I have a problem that I think it's easiest to solve with awk but I wrapped my head around it.
Inside a file I have repeating output like this:
....
Name="BgpIpv4RouteConfig_XXX">
<Ipv4NetworkBlock id="13726"
StartIpList="x.y.z.t"
PrefixLength="30"
NetworkCount="10000"
... other output
then this block will repeat.
a)I want to match on BGPIpv4Route.*, then skip 2 lines (the "n" keyword of awk), then when reaching Prefix Length:
- either replace it with random (25,30)
or
- better but I guess harder (no idea came to mind for keeping track of what was used and looping among /25../30) -> first occurrence /25, second one /26...till /30, then rollback to /25
b) then next line with NetworkCount depending on the new value of PrefixCount calculate it as 65536 / 2^(32-Prefix Count)
eg: if PrefixCount on this occurrence was replaced with /25, then NetworkCount on the line following it = 65536 / 2 ^ 7 = 65536 / 128 = 512
I found some examples with inserting/changing a line after one that matched (or with a counter variable X lines below the match) but I got a bit confused with the value generation part and also with the changing of two lines where one is depending on the other.
Not sure I made any sense...my head is a bit overwhelmed with what I'm finding everywhere right now.
Thanks in advance!
this should do
$ awk 'BEGIN {q="\""; FS=OFS="="; n=split("25=26=27=28=29=30",ps)}
/BgpIpv4Route/ {c=c%n+1}
/PrefixLength/ {$2=q ps[c] q}
/NetworkCount/ {$2=q 65536/2^(32-ps[c]) q}1' file
perhaps minimize computation by changing to 2^(ps[c]-16)
If there are free standing PrefixLength and NetworkCount attributes perhaps you need to qualify them for each BgpIpv4Route context.

Performing exact match when comparing variables in SPSS Statistics

I'm wondering if there's a way for me to perform an exact match compare in SPSS. Currently, using the following will return system missing (null) in cases where one variable is sysmis:
compute var1_comparison = * Some logic here.
compute var1_check = var1 = var1_comparison.
The results look like this (hypens representing null values):
ID var1 var1_comparison var1_check
1 3 3 1
2 4 3 0
3 - 2 -
4 1 1 1
5 - - -
What I want is this:
ID var1 var1_comparison var1_check
1 3 3 1
2 4 3 0
3 - 2 0
4 1 1 1
5 - - 1
Is this possible using just plain SPSS syntax? I'm also open to using the Python extension, though I'm not as familiar with it.
Here's a slightly different approach, using temporary scratch variables (prefixed by a hash (#)):
recode var1 var1_comparison (sysmis=-99) (else=copy) into #v1 #v2.
compute Check=(#v1 = #v2).
This is to recreate your example:
data list list/ID var1 var1_comparison.
begin data
1, 3, 3
2 , 4, 3
3, , 2
4, 1, 1
5, ,
end data.
Now you have to deal separately with the situation where both values are missing, and then complete the calculation in all other situations:
do if missing(var1) or missing(var1_comparison).
compute var1_check=(missing(var1) and missing(var1_comparison)).
else.
compute var1_check = (var1 = var1_comparison).
end if.

Resources