SPSS Gurus;
=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
I have a date variable that changes from a date to a string variable intermittently. How would I go about creating a conditional statement to choose between two syntax statements to perform several different functions (e.g. creating a numeric Year, etc...). Its probably very simple, but I'm old and lazy... Thanks, JT |
To branch conditionally on the variable type requires programmability. Here is an example of how to do that. The first block records the type of the variable bdate (true date variables are numeric, which is type 0), and the second block illustrates using that information to run different sets of commands. Note the use of triple quotes around the syntax, which can consist of one or more commands. If there is a lot of code, you might want to use INSERT here. begin program. import spss, spssaux datetype = spssaux.VariableDict("bdate").variableType("bdate") print datetype end program. begin program. if datetype == 0: spss.Submit("""descriptives variable=bdate.""") else: spss.Submit("""freq bdate.""") end program. On Wed, Mar 6, 2019 at 8:14 AM Turner, John <[hidden email]> wrote:
|
Before Jon's answer, I was puzzled by the question. Now, I guess that the
question has to be about the processing of supposed-to-be-identical file
formats, for data received from multiple sources.
I would use Jon's programmability to create two new, named versions of
the unreliably-formatted date variable in the file, so that all later jobs could
unambiguously use a string or a date.
--
Rich Ulrich
From: SPSSX(r) Discussion <[hidden email]> on behalf of Jon Peck <[hidden email]>
Sent: Wednesday, March 6, 2019 1:18 PM To: [hidden email] Subject: Re: How to use a conditional statement for different field types To branch conditionally on the variable type requires programmability. Here is an example of how to do that.
The first block records the type of the variable bdate (true date variables are numeric, which is type 0), and the second block illustrates using that information to run different sets of commands. Note
the use of triple quotes around the syntax, which can consist of one or more commands. If there is a lot of code, you might want to use INSERT here.
begin program.
import spss, spssaux
datetype = spssaux.VariableDict("bdate").variableType("bdate")
print datetype
end program.
begin program.
if datetype == 0:
spss.Submit("""descriptives variable=bdate.""")
else:
spss.Submit("""freq bdate.""")
end program.
On Wed, Mar 6, 2019 at 8:14 AM Turner, John <[hidden email]> wrote:
|
Make sense. It might be sufficient to just use ALTER TYPE to make the variable a numeric date. If it is already numeric, this will just be a no-op. For example, alter type bdate(ADATE10). would convert a string date with format ADATE10 to its numeric equivalent. On Wed, Mar 6, 2019 at 11:29 AM Rich Ulrich <[hidden email]> wrote:
|
In reply to this post by Turner, John E. (VADOC)
John Turner wrote,
=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
"I have a date variable that changes from a date to a string variable intermittently. How would I go about creating a conditional statement to choose between two syntax statements to perform several different functions (e.g. creating a numeric Year, etc...)." First of all, where is the source? SPSS variables can't change types from case to case within a file; some other systems (like Excel columns) can. When you load into SPSS does it come in as one date variable that is often missing, or one string variable, or two variables, one date and one string? Second, I'd say the preferred strategy is to get all the dates as a single SPSS date variable, and after that use the SPSS date functions to get the numeric year, etc. (OK, you'd probably got that far.) That depends on how you can import the original data. I'd try for either, A. A single string variable, in which the dates in the source are converted to strings in some suitable format, or B. Two variables, one an SPSS date (which will be missing for the inputs that are strings) and one SPSS string variable -- this will probably require loading the original data twice, In either case, convert to a single SPSS date variable. ALTER TYPE may not be good enough, if the formats of the string-format dates are too wildly varying. In case A., write a DO IF that applies INPUT with a succession of formats to try to convert the string date to an SPSS date; in case B., do the same for the string variable, but ONLY for those cases where the date variable is not missing (test using the MISSING function). In neither case, do you have to use the same conditional for two different input types. |
Thanks for everyone's input. The agency I work for has an extract of data that is pulled down monthly and we pull data off of that extract. Occasionally, the data format will change on a date that is key to a very long syntax that I run (8 hours of processing time). So, I wanted to write a syntax solution that would take either possibility into consideration and save me the time of re-running the syntax. I believe everyone's input so far will work well and I appreciate the help. JT On Wed, Mar 6, 2019 at 2:56 PM Richard Ristow <[hidden email]> wrote:
|
Free forum by Nabble | Edit this page |