Changing values of a lot of variables - spss

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.

Related

Systematically renaming variables in SPSS (without Python)

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

SPSS: Recode command

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.

Labeled constants in LaTeX

I have several lemmas in which I specify constants $C_1$, $C_2$, and so forth for later reference. Naturally, this is annoying when I later insert a new constant definition in the middle. What I'd like is a macro that lets me assign labels to constants and handles the numbering for me. I'm thinking something along the lines of
%% Pseudocode
\begin{lemma}
\newconstant{important-bound}
We will show that $f(x) \le \ref{important-bound} g(x)$ for all $x$.
\end{lemma}
Is this possible?
Expanding on rcollyer's suggestions of using a counter:
%counter of current constant number:
\newcounter{constant}
%defines a new constant, but does not typeset anything:
\newcommand{\newconstant}[1]{\refstepcounter{constant}\label{#1}}
%typesets named constant:
\newcommand{\useconstant}[1]{C_{\ref{#1}}}
(This code was edited to allow labels longer than one character)
And here is a code snippet that seems to work:
I want to define two constants:\newconstant{A}\newconstant{B}$\useconstant{A}$ and
$\useconstant{B}$. Then I want to use $\useconstant{A}$ again.
What you're looking for is to create your own counter.
Expanding on Aniko's answer, I used
this layered macro so that it created a shorthand for the label,
\newcounter{constant}
\newcommand{\newconstant}[1]{\refstepcounter{constant}\label{#1}}
\newcommand{\useconstant}[1]{C_{\ref{#1}}}
\newcommand{\defconstant}[1]{ \newconstant{c_#1}\expandafter\newcommand\csname c#1\endcsname{\useconstant{c_#1}} } %
So to use this, you would then do
\defconstant{a}
\defconstant{b}
There exist constant $\ca$ and $\cb$ such that ....
careful not to overwrite existing commands (i'm sure it would warn you anyhow)

TeX edef macro blues

I spent some time trying to write a 'helper' macro to test a parameter for a new value, else use the existing value -- default values exist for all parameter positions.
I wanted to be able to write:
\foo{left}{nil}{}{20pt}
so that the second parameter would used its current value but the third value would be the value empty string. I wanted to use the notation:
\edef\pA{\isnil{#1}{\pA){#1}} % one for each parameter
I defined \isnil like so:
\def\nil{nil}
\def\isnil#1#2#3{%
\edef\nilTest{#1}%
\ifx\nilTest\nil#2\else#3\fi
}
but when I tried to run it, TeX complained that \nilTest is an undefined control sequence.
That is true of course, but I want \pA to hold a value, not a recipe for a value, so it must be an \edef which means that all the macro test will be expanded but while will the \edef not protect the \nilTest -- is this a place to use \noexpand -- that did not seem to work for me.
EDIT: no digits in \cs names (yeah, I knew that.)
Why doesn't your solution work? \edef\pA{\isnil{#1}{\pA){#1}} expands \isnil and gets \edef\nilTest{.... Now \edef is not expandable and falls into a sequence of \pA as the first element. An attempt to expand the next macro \nilTest fails.
Use \setpar from the following code to change your parameter.
\def\nil{nil}
\def\setpar#1#2{%
\edef\nilTest{#2}%
\ifx\nilTest\nil\else\let#1\nilTest\fi}
\def\first{old first}
\def\second{old second}
\setpar \first{nil}
\setpar \second{new}
first = ``\first'', second = ``\second''
P.S. Do not use digits in your macro.

"do" in "do Application.Run(form)" sentence

What is the difference between
do Application.Run(form)
and, simply:
Application.Run(form) ?
What is the role of do keyword in the first sentence?
Whereas 'do' was a required keyword in many places in the language in some of the earlier releases, nowadays you rarely need 'do'. The remaining exceptions that I can think of are that 'do' is still part of loop syntax (e.g. "while e1 do e2") and if you want to put an assembly-level attribute or an attribute on the startup method, you can put the attribute before the explicit 'do' of a final code block in a module. Often times in F# samples you'll see
[<STAThread>]
do Application.Run(form)
as the last two lines of a file, and I think the 'do' is still required there in order to be able to attach the attribute on the line above it.
I think it's just a holdover - like how you can still CALL a sub or SET a variable instead of just doing those things directly, as in:
SET varname = 5
CALL mysub()
Versus just:
varname = 5
mysub()
In other words, I don't think it matters, and the compiler just discards it.

Resources