What does this syntax mean?
Recode
variable1 variable2 (ELSE=SYSMIS) .
EXECUTE .
I cant seem to find any example with the recode command where only variables are stated and no values to recode. Does this mean it will just copy whatever value of variable1 and variable2 for a given record?
That is a roundabout way of setting the variables to system missing. It's equivalent to
compute variable1 = $sysmis.
compute variable2 = $sysmis.
But it does both in a single command.
It works because without a recode list, everything falls into the ELSE category.
Related
I'm looking for a solution to rename variables in SPSS. I can't use Python because of software restrictions at my workplace.
The goal is to rename variables into "oldname_new".
I tried "do repeat" like this, but it can't be combined with the rename function.
do repeat x= var1 to var100.
rename var (x=concat("x","_new")).
end repeat print.
exe.
Also, I figured that even without the do repeat, the rename command doesn't allow concat and similar commands? Is that correct?
So, is there any solution for this in SPSS?
As you found out you can't use rename within a do repeat loop.
SPSS macro can do this -
define DoNewnames ()
rename vars
!do !v=1 !to 100 !concat("var", !v, " = var", !v, "_new") !doend .
!enddefine.
* now the macro is defined, we can run it.
DoNewnames .
EDIT:
The code above is good for a set of variables with systematic names. In case the names are not systematic, you will need a different macro:
define DoNewnames (varlist=!cmdend)
rename vars
!do !v !in(!varlist) !concat(!v, " = ", !v, "_new") !doend .
!enddefine.
* Now in this case you need to feed the variable list into the macro.
DoNewnames varlist = age sex thisvar thatvar othervar.
If you want to see the syntax generated by the macro (like you did with end repeat print) you can run this before running the macro:
set mprint on.
EDIT 2:
As the OP says - the last macro requires naming all the variables to be renamed, which is a hassle if there are many. So the next code will get them all automatically without naming them individually. The process - as described in #petit_dejeuner's comment - creates a new data set that contains each original variable as an observation, and the original variable name as a value (=meta information about the variables, like a codebook). This way, you can recode the variable name into the renaming syntax.
dataset name orig.
DATASET DECLARE varnames.
OMS /SELECT TABLES /IF COMMANDS=['File Information'] SUBTYPES=['Variable Information']
/DESTINATION FORMAT=SAV OUTFILE='varnames' VIEWER=NO.
display dictionary.
omsend.
dataset activate varnames.
string cmd (a50).
compute cmd=concat("rename vars ", rtrim(var1), " = ", rtrim(var1), "_new .").
* Before creating the rename syntax in the following line, this is your chance to remove variables from the list which you do not wish to rename (using "select if" etc' on VAR1).
write out="my rename syntax.sps" /cmd.
dataset activate orig.
insert file="my rename syntax.sps" .
A couple of notes:
Before writing to (and inserting from) "my rename syntax.sps" you may need to add a writable path in the file name.
This code will rename ALL the variable in the dataset. If you want to avoid some of the variables - you should filter them in the variable list before writing out to "my rename syntax.sps" (see where I point this out in the code).
I want to change the missing values of a lot of variables into 0.I've written this code(There are a lot more of variables,it's just an example):
RECODE variable1 ($SYSMIS = 0).
EXECUTE.
RECODE variable2 ($SYSMIS = 0).
EXECUTE.
RECODE variable3 ($SYSMIS = 0).
EXECUTE.
Is there a simpler way to do the same?Like a while in programming or something like that
Thanks you very much
Your first step should be to combine all your commands into one - this can be done whatever the order of the variables - just explicitly mention all the variable names in the command (as david mentioned):
RECODE variable1 variable2 variable3 variable4 .... (sysmis=0).
This will save quite a lot of code already.
now, as Jignesh mentioned - for variables that are in consecutive order, you can use to:
RECODE variable1 to variable3 (sysmis=0).
or also an example with two groups of consecutive variables and two additional separate variables:
RECODE variable1 TO variable23 var1 TO var7 AnotheVar3 AnotheVar12 (sysmis=0).
definitely no need for looping commands here.
If the desired target variables are in consecutive order in your active dataset you can use the keyword TO.
For example:
RECODE V1 to V3 (SYSMIS=0).
Note your incorrect use of keyword "SYSMIS" in the RECODE command and the corrected in example code provided.
Else you will have to resort to using DO REPEAT or DEFINE/ENDDEFINE.
In addition to Jignesh's response, If all of the variables are Numeric you can use ALL.
RECODE ALL(SYSMIS=0).
DEFINE !ENDDEFINE would be Overkill, DO REPEAT doesn't buy you anything over explicitly listing the variables in the RECODE command.
I inject environmental variable from a file myprop.property that has the contents:
var1=y
var2=y
The build steps:
1. Inject environment variables:
Property File Path:${JENKINS_HOME}/myprop.propertie
Execute Windows batch command (to verify variable injected successully)
echo var1 = %var1% echo var2 = %var2%
Condition steps (multiple)
Run?: Boolean condition
Token: ${ENV,var="var1"}||${ENV,var="var2"}
Steps to run if condition is met: echo Yes, works!
Run the build, the condition in step 3 never met while step 2 display the correct values of the variables.
I have tried the conditions and operators:
var1=y, var2=y: ${ENV,var="var1"}||${ENV,var="var2"}
var1=y, var2=y: ${ENV,var="var1"}|${ENV,var="var2"}
var1=y, var2=n: ${ENV,var="var1"}||${ENV,var="var2"}
var1=y, var2=n: ${ENV,var="var1"}|${ENV,var="var2"}
Uppercase or lower case of the values do not make any difference. I am running jenkins 1.641 on windows 7 pro.
If I use only one e.g. ${ENV,var="var1"} in the token field, it works as expected.
Try this instead, It worked for me, we need to mention Boolean Condition with Token mentioned in the image.
This works, change step 3 to:
Run?: Or
Boolean condition
Token: ${ENV,var="var1"}
Or
Boolean condition
Token: ${ENV,var="var2"}
Never
these are actually three conditions. the execution asserts the first condition first, if it's met, stop checking; if not met, asserts the second; and so on until the last condition that is Never, meaning if no condition is met, stop executing the step.
I see my post attracted lots interests. Here are what I eventually worked around.
Use Regular expression match seems to be more readable than using Boolean Condition.
If the logic is an "OR",
? = Regular expression match
expression = ${ENV,var="var1"}|${ENV,var="var2"}
Label = Y
Note that the logic operator is a single '|', not double '||'.
if the logic is an "AND",
? = Regular expression match
expression = ${ENV,var="var1"}${ENV,var="var2"}
Label = YY
The expression is just concatenation of the two variables, and the Label is so as well.
This is for code:
set source_failed = `cat mine.log`
set dest_failed = `cat their.log`
foreach t ($source_failed)
set isdiff = 0
set sflag = 0
foreach t2 ($dest_failed)
if ($t2 == $t) then
set sflag = 1
break
endif
end
...
end
Problem is that the inner foreach loop runs okay for first few 10 iterations. After that iteration, suddenly I got
foreach: no match
Moreover, I am iterating over the array of strings, not files. What is the reason behind this error?
The problem is (probably) that mine.log and/or their.log contain special globbing characters, such as * or ?. The shell will try to expand this to a file. There are no matches for this accidental pattern, and hence the error "no match".
The easiest way to prevent this behaviour is to add set noglob to the top. From tcsh(1):
noglob If set, Filename substitution and Directory stack substitution
(q.v.) are inhibited. This is most useful in shell scripts
which do not deal with filenames, or after a list of filenames
has been obtained and further expansions are not desirable.
You can re-enable this behaviour by using set glob.
Alternativly, you can use :q. From tcsh(1):
Unless enclosed in `"' or given the `:q' modifier the results of variable
substitution may eventually be command and filename substituted.
[..]
When the `:q' modifier is applied to a substitution the variable will expand
to multiple words with each word sepa rated by a blank and quoted to
prevent later command or filename sub stitution.
But you need to be very careful about quoting when you use the variable. In the below example, the echo command will fail if you don't add quotes (set noglob is much easier):
set source_failed = `cat source`
foreach t ($source_failed:q)
echo "$t"
end
Lets say for example I have a variable in SPSS named var1 containing ones and zeros. And I have another variable var2 which I want to set to sysmis if var1 = 1.
How can I do that in SPSS?
The line
IF (var1=1) var2 = SYSMIS.
produces an error.
The trick here is to use the system variable $SYSMIS.
IF (var1=1) var2 = $SYSMIS.
$SYSMIS is documented in Variables section under Universals section in the Syntax Reference Guide.