question about number of years applied

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

question about number of years applied

Miriam L. Gerver
Hi all,

I feel like there must be a way to generate this variable, but have
been spinning my wheels and can't come up with a solution.

I have a dataset with department ID numbers, and each department has
grant years for which they applied for a grant (denoted by year
number). It's easier for me to show what the data looks like rather
than trying to describe it:

Dept. #        1st grant     2nd grant     3rd grant     4th grant     5th grant
1                   2001          2001           2002             2002
2                   2001          2002           2002             2002
3                   2001          2001           2002             2002
         2003
4                   2002          2003           2004             2005
5                   2004          2004           2005             2005
6                   2001          2002           2003             2004
         2005
7                   2003          2004           2004             2004

I'd like to generate a variable that indicates the number of years
that a department has applied for grants. For example, Dept. #1 has
applied for 4 grants, but has only applied in 2 year, Dept. 4 has
applied for 4 grants, but has applied in 4 years.

This is how I'd like the new number of years variable to look:

Dept. #      years applied
1                  2
2                  2
3                  3
4                  4
5                  2
6                  5
7                  2

If you have any idea of how I'd go about doing this, I would greatly
appreciate your feedback!

Thank you,
Miriam Gerver
Reply | Threaded
Open this post in threaded view
|

Re: question about number of years applied

meljr
A little down and dirty, but this works.
meljr

GET
  FILE='C:\SPSS Training\Vectors\agg_grants.sav'.

COUNT V2001=grant1 grant2 grant3 grant4 grant5  (2001).
COUNT V2002=grant1 grant2 grant3 grant4 grant5  (2002).
COUNT V2003=grant1 grant2 grant3 grant4 grant5  (2003).
COUNT V2004=grant1 grant2 grant3 grant4 grant5  (2004).
COUNT V2005=grant1 grant2 grant3 grant4 grant5  (2005).
if (v2001 >=1) v2001 = 1.
if (v2002 >=1) v2002 = 1.
if (v2003 >=1) v2003 = 1.
if (v2004 >=1) v2004 = 1.
if (v2005 >=1) v2005 = 1.


compute yrs_ap = sum(v2001 to v2005).
fre var = yrs_ap.


Miriam L. Gerver wrote
Hi all,

I feel like there must be a way to generate this variable, but have
been spinning my wheels and can't come up with a solution.

I have a dataset with department ID numbers, and each department has
grant years for which they applied for a grant (denoted by year
number). It's easier for me to show what the data looks like rather
than trying to describe it:

Dept. #        1st grant     2nd grant     3rd grant     4th grant     5th grant
1                   2001          2001           2002             2002
2                   2001          2002           2002             2002
3                   2001          2001           2002             2002
         2003
4                   2002          2003           2004             2005
5                   2004          2004           2005             2005
6                   2001          2002           2003             2004
         2005
7                   2003          2004           2004             2004

I'd like to generate a variable that indicates the number of years
that a department has applied for grants. For example, Dept. #1 has
applied for 4 grants, but has only applied in 2 year, Dept. 4 has
applied for 4 grants, but has applied in 4 years.

This is how I'd like the new number of years variable to look:

Dept. #      years applied
1                  2
2                  2
3                  3
4                  4
5                  2
6                  5
7                  2

If you have any idea of how I'd go about doing this, I would greatly
appreciate your feedback!

Thank you,
Miriam Gerver
Reply | Threaded
Open this post in threaded view
|

Re: question about number of years applied

Mahbub Khandoker
In reply to this post by Miriam L. Gerver
Hi Miriam,
You can try the following syntax. Basically I did count years in the grant year and recode them to 1 and then count again.
Some one in the list may come up with a concise way.

DATA LIST free/ Dept(n1) grant1(n4) grant2(n4) grant3(n4) grant4(n4) grant5(n4).
Begin data
1 2001 2001 2002 2002 0
2 2001 2002 2002 2002 0
3 2001 2001 2002 2002 2003
4 2002 2003 2004 2005 0
5 2004 2004 2005 2005 0
6 2001 2002 2003 2004 2005
7 2003 2004 2004 2004 0
End data.
Execute.

COUNT
Y2001 = grant1 to grant5(2001).
EXECUTE .
COUNT
Y2002 = grant1 to grant5(2002).
EXECUTE .
COUNT
Y2003 = grant1 to grant5(2003).
EXECUTE .
COUNT
Y2004 = grant1 to grant5(2004).
EXECUTE .
COUNT
Y2005 = grant1 to grant5(2005).
EXECUTE .

RECODE
  Y2001 Y2002 Y2003 Y2004 Y2005 (1 thru Highest=1)  .
EXECUTE .

COUNT
Years_Applied = Y2001 to Y2005(1).
EXECUTE .

Thanks,
Mahbub

 -----Original Message-----
From:   SPSSX(r) Discussion [mailto:[hidden email]]  On Behalf Of Miriam L. Gerver
Sent:   22-Mar-07 11:23 AM
To:     [hidden email]
Subject:             question about number of years applied

Hi all,

I feel like there must be a way to generate this variable, but have
been spinning my wheels and can't come up with a solution.

I have a dataset with department ID numbers, and each department has
grant years for which they applied for a grant (denoted by year
number). It's easier for me to show what the data looks like rather
than trying to describe it:

Dept. #        1st grant     2nd grant     3rd grant     4th grant     5th grant
1                   2001          2001           2002             2002
2                   2001          2002           2002             2002
3                   2001          2001           2002             2002
         2003
4                   2002          2003           2004             2005
5                   2004          2004           2005             2005
6                   2001          2002           2003             2004
         2005
7                   2003          2004           2004             2004

I'd like to generate a variable that indicates the number of years
that a department has applied for grants. For example, Dept. #1 has
applied for 4 grants, but has only applied in 2 year, Dept. 4 has
applied for 4 grants, but has applied in 4 years.

This is how I'd like the new number of years variable to look:

Dept. #      years applied
1                  2
2                  2
3                  3
4                  4
5                  2
6                  5
7                  2

If you have any idea of how I'd go about doing this, I would greatly
appreciate your feedback!

Thank you,
Miriam Gerver
Reply | Threaded
Open this post in threaded view
|

Re: question about number of years applied

Richard Ristow
In reply to this post by Miriam L. Gerver
At 11:23 AM 3/22/2007, Miriam L. Gerver wrote (see syntax below, for
the question):

I like Mahbub Khandoker's code, but I was already writing this, another
approach. It does not assume grants are applied for only in 2001-2005,
which Mahbub's does; it does require that grant years are listed in
non-decreasing order, as Mahbub's code does not.

SPSS 15 draft output:

List
|-----------------------------|---------------------------|
|Output Created               |22-MAR-2007 13:53:04       |
|-----------------------------|---------------------------|
Dept# Grant1yr Grant2yr Grant3yr Grant4yr Grant5yr

    1    2001     2001     2002     2002        .
    2    2001     2002     2002     2002        .
    3    2001     2001     2002     2002     2003
    4    2002     2003     2004     2005        .
    5    2004     2004     2005     2005        .
    6    2001     2002     2003     2004     2005
    7    2003     2004     2004     2004        .

Number of cases read:  7    Number of cases listed:  7


*  "I'd like to generate a variable that indicates the number of  .
*  years that a department has applied for grants. For example,   .
*  Dept. #1 has applied for 4 grants, but only applied in 2 years".

NUMERIC     YrsAppld (F3).
VAR LABEL   YrsAppld 'Number of years in which grant applied for'.
VAL LABEL   YrsAppld  99 'Yrs out of order'.
MISSING VAL YrsAppld (99).

NUMERIC     #LtstYr  (F4).

COMPUTE     YrsAppld = 0.
COMPUTE     #LtstYr  = $SYSMIS.

VECTOR GrantYr=Grant1yr TO Grant5yr.
*  The following logic relies on the years being in non-decreasing.
*  order in the vector. It does check, and return an error code   .
*  if that's not the case.                                        .
LOOP #Grant = 1 TO 5.
.  DO IF   NOT MISSING(GrantYr(#Grant)).
.     DO IF               MISSING(#LtstYr)
             OR GrantYr(#Grant) GT #LtstYr.
.        COMPUTE #LtstYr  = GrantYr(#Grant).
.        IF NOT MISSING (YrsAppld)
                  YrsAppld = YrsAppld + 1.
.     ELSE IF  GrantYr(#Grant) LT #LtstYr.
.        COMPUTE YrsAppld = 99.
.     END IF.
.  END IF.
END LOOP.

LIST.

List
|-----------------------------|---------------------------|
|Output Created               |22-MAR-2007 13:53:06       |
|-----------------------------|---------------------------|
Dept# Grant1yr Grant2yr Grant3yr Grant4yr Grant5yr YrsAppld

    1    2001     2001     2002     2002        .        2
    2    2001     2002     2002     2002        .        2
    3    2001     2001     2002     2002     2003        3
    4    2002     2003     2004     2005        .        4
    5    2004     2004     2005     2005        .        2
    6    2001     2002     2003     2004     2005        5
    7    2003     2004     2004     2004        .        2

Number of cases read:  7    Number of cases listed:  7
===================
APPENDIX: Test data
===================
* ............   Test data               ............          .
DATA LIST LIST SKIP=1
    /Dept#  Grant1yr    Grant2yr    Grant3yr    Grant4yr    Grant5yr.
BEGIN DATA
Dept. #    1st grant   2nd grant   3rd grant   4th grant   5th grant
1             2001        2001        2002        2002
2             2001        2002        2002        2002
3             2001        2001        2002        2002        2003
4             2002        2003        2004        2005
5             2004        2004        2005        2005
6             2001        2002        2003        2004        2005
7             2003        2004        2004        2004
END DATA.
FORMATS Dept# (F3)
        /Grant1yr TO Grant5yr (F4).
Reply | Threaded
Open this post in threaded view
|

find command

Graham Wright-2
In reply to this post by Mahbub Khandoker
Something seems to be going wrong with the find command in my version of
SPSS 14. There are times when I am searching for a certain word in a
long sytnax file and I know it's there but the find command can't seem
to find it. I can even copy a word like "freq" that's in the file a
hundred times and past it into the find window and SPSS still tells me
that it never appears in the file. This seems to only happen after I've
searched for other commands first. Like I will search for "merge" and
the find command will work find, but then it won't be able to find
anything. I have to shut SPSS down and restart it to get the find
function to work again.  I've made sure I'm searching to correct file,
that my cursor is at the top of the syntax file before I start
searching, that I'm not highlighting any text etc and it still does it,
even if I have my cursor on the exact word I'm searching for. I have to
past the entire syntax file into text pad to find anything now. Does any
one know why this is happening?

-Graham
Reply | Threaded
Open this post in threaded view
|

Re: find command

peter link
Graham -

I've experienced what you describe and I use SPSS v11.5.  It can be very
frustrating.  My solution has been exactly what you seem to be doing, which
is far from ideal.  I am not sure what causes the problem or what the fix
is, but I would be very interested.

Peter

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]]On Behalf Of
Graham Wright
Sent: Thursday, March 22, 2007 11:21 AM
To: [hidden email]
Subject: find command


Something seems to be going wrong with the find command in my version of
SPSS 14. There are times when I am searching for a certain word in a
long sytnax file and I know it's there but the find command can't seem
to find it. I can even copy a word like "freq" that's in the file a
hundred times and past it into the find window and SPSS still tells me
that it never appears in the file. This seems to only happen after I've
searched for other commands first. Like I will search for "merge" and
the find command will work find, but then it won't be able to find
anything. I have to shut SPSS down and restart it to get the find
function to work again.  I've made sure I'm searching to correct file,
that my cursor is at the top of the syntax file before I start
searching, that I'm not highlighting any text etc and it still does it,
even if I have my cursor on the exact word I'm searching for. I have to
past the entire syntax file into text pad to find anything now. Does any
one know why this is happening?

-Graham
Reply | Threaded
Open this post in threaded view
|

Re: find command

Ana Luebbers
In reply to this post by Graham Wright-2
Hi,

This seems to be a well known problem to SPSS.  This is what I found on
the SPSS website:

************************
" Problem Description:  I'm using SPSS 14.0 for Windows and I'm having
trouble with the Find/Replace function in the Syntax Editor. The one
thing that I can consistently replicate is that a string that starts in
the first character of the first line of a syntax file seems to be
invisible to the Find dialog box.

For instance, I have a syntax file that starts with a GET FILE command
in that first position, first line -- and that's the only appearance of
"get" in the file. If I try to Find (i.e., Edit->Find, or clicking the
Binoculars in the toolbar) the string "get" the application responds
with a "Not Found" message. If I then put either a single extra space or
an extra line before the GET FILE command, then the Find function can
find the "get" string. This problem is replicable -- it happens every
single time, in 14.0.1 as well.

What's wrong?  

Resolution Subject: This appears to be a defect

Resolution Description:
This is a defect and has been fixed in SPSS 15.0 for Windows. As a
workaround if you have extended Find/Replaces to do as part of your
editing, you could select all the text in your syntax file, and
copy/paste to a simple text editor, such as NotePad or Programmer's File
Editor. Perform your editing and then copy/paste your edited code back
to the SPSS Syntax Editor and save.

There have been other reported issues with the Find feature in SPSS 14.0
and 15.0 that we have not been able to consistently replicate, and thus
cannot be certain have been fixed along with the other replicable
issues. One example involves a file with over 50 instances of a
particular string, but sporadically the Find/Replace dialog can only
"see" the first instance -- clicking Find Next in the dialog box does
not advance the cursor to the next instance. So far SPSS Development has
not been able to replicate this issue in a controlled environment, but
user reports indicate that closing and re-opening the syntax file does
seem to "re-set" the Find dialog's functionality. SPSS 16.0 for Windows
will use a new syntax editor and all Find/Replace functionality should
work as expected in that release.

We apologize for the inconvenience. "

*******************************


Ana

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
peter link
Sent: Thursday, March 22, 2007 2:43 PM
To: [hidden email]
Subject: Re: find command

Graham -

I've experienced what you describe and I use SPSS v11.5.  It can be very
frustrating.  My solution has been exactly what you seem to be doing,
which
is far from ideal.  I am not sure what causes the problem or what the
fix
is, but I would be very interested.

Peter

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]]On Behalf Of
Graham Wright
Sent: Thursday, March 22, 2007 11:21 AM
To: [hidden email]
Subject: find command


Something seems to be going wrong with the find command in my version of
SPSS 14. There are times when I am searching for a certain word in a
long sytnax file and I know it's there but the find command can't seem
to find it. I can even copy a word like "freq" that's in the file a
hundred times and past it into the find window and SPSS still tells me
that it never appears in the file. This seems to only happen after I've
searched for other commands first. Like I will search for "merge" and
the find command will work find, but then it won't be able to find
anything. I have to shut SPSS down and restart it to get the find
function to work again.  I've made sure I'm searching to correct file,
that my cursor is at the top of the syntax file before I start
searching, that I'm not highlighting any text etc and it still does it,
even if I have my cursor on the exact word I'm searching for. I have to
past the entire syntax file into text pad to find anything now. Does any
one know why this is happening?

-Graham
Reply | Threaded
Open this post in threaded view
|

Re: question about number of years applied

Peck, Jon
In reply to this post by Richard Ristow
Ok.  Here's the Python way using SPSS 15 (you knew that was coming, right?).  Others have posted pure SPSS ways of solving this problem that work, but the code below illustrates a general solution to counting distinct values in a group of variables without any extra assumptions.

Explanation after the code.

begin program.
import spss, spssaux, spssdata

vardict = spssaux.VariableDict()
curs = spssdata.Spssdata(accessType='w', indexes=vardict.range("Grant1yr", "Grant5yr"))
curs.append(spssdata.vdef("YearsApplied", vlabel="Number of years with grant app", vfmt=("F",5,0)))
curs.commitdict()

for case in curs:
    yrs = set([])
    for v in case:
        yrs.add(v)
    curs.casevalues([len(yrs)])
curs.CClose()
end program.

Explanation:

vardict... creates a dictionary in Python of the SPSS variables.
curs = ...  gets a write cursor to the data and defines grant1yr TO grant5yr as the variables to retrieve making use of the range method of VariableDict objects.
curs.append ... defines a new numeric variable and assigns a variable label and format.

for case in curs ... loops over the data.
yrs... makes use of the built-in set feature of Python, starting with an empty set for each case.
for v ... adds each value of the SPSS variables retrieved to the set.  It is the  nature of sets that the elements are all distinct.
So the curs.casevalues... line stores the size of the set, i.e., the number of distinct values in the new variable.

-Jon Peck
SPSS


-----Original Message-----
Dept. #        1st grant     2nd grant     3rd grant     4th grant     5th grant
1                   2001          2001           2002             2002
2                   2001          2002           2002             2002
3                   2001          2001           2002             2002
         2003
4                   2002          2003           2004             2005
5                   2004          2004           2005             2005
6                   2001          2002           2003             2004
         2005
7                   2003          2004           2004             2004

I'd like to generate a variable that indicates the number of years that a department has applied for grants. For example, Dept. #1 has applied for 4 grants, but has only applied in 2 year, Dept. 4 has applied for 4 grants, but has applied in 4 years.
Reply | Threaded
Open this post in threaded view
|

Re: question about number of years applied

Miriam L. Gerver
Thanks everyone for all your help!

Miriam


On 3/22/07, Peck, Jon <[hidden email]> wrote:

> Ok.  Here's the Python way using SPSS 15 (you knew that was coming, right?).  Others have posted pure SPSS ways of solving this problem that work, but the code below illustrates a general solution to counting distinct values in a group of variables without any extra assumptions.
>
> Explanation after the code.
>
> begin program.
> import spss, spssaux, spssdata
>
> vardict = spssaux.VariableDict()
> curs = spssdata.Spssdata(accessType='w', indexes=vardict.range("Grant1yr", "Grant5yr"))
> curs.append(spssdata.vdef("YearsApplied", vlabel="Number of years with grant app", vfmt=("F",5,0)))
> curs.commitdict()
>
> for case in curs:
>     yrs = set([])
>     for v in case:
>         yrs.add(v)
>     curs.casevalues([len(yrs)])
> curs.CClose()
> end program.
>
> Explanation:
>
> vardict... creates a dictionary in Python of the SPSS variables.
> curs = ...  gets a write cursor to the data and defines grant1yr TO grant5yr as the variables to retrieve making use of the range method of VariableDict objects.
> curs.append ... defines a new numeric variable and assigns a variable label and format.
>
> for case in curs ... loops over the data.
> yrs... makes use of the built-in set feature of Python, starting with an empty set for each case.
> for v ... adds each value of the SPSS variables retrieved to the set.  It is the  nature of sets that the elements are all distinct.
> So the curs.casevalues... line stores the size of the set, i.e., the number of distinct values in the new variable.
>
> -Jon Peck
> SPSS
>
>
> -----Original Message-----
> Dept. #        1st grant     2nd grant     3rd grant     4th grant     5th grant
> 1                   2001          2001           2002             2002
> 2                   2001          2002           2002             2002
> 3                   2001          2001           2002             2002
>          2003
> 4                   2002          2003           2004             2005
> 5                   2004          2004           2005             2005
> 6                   2001          2002           2003             2004
>          2005
> 7                   2003          2004           2004             2004
>
> I'd like to generate a variable that indicates the number of years that a department has applied for grants. For example, Dept. #1 has applied for 4 grants, but has only applied in 2 year, Dept. 4 has applied for 4 grants, but has applied in 4 years.
>
Reply | Threaded
Open this post in threaded view
|

Re: find command

Butler, Deborah {FLNA}
In reply to this post by Ana Luebbers
Man, anyone else find that frustrating?!

I have a problem being told to buy an upgrade in order to fix a 'bug' on
a feature which SHOULD have been working to begin with.  I think we
should at least get a patch for it.  I certainly don't mind upgrading in
order to get a new FEATURE, but not an existing one fixed.


Debbie Butler
SCP Modeling Analyst
Frito Lay Operations
972.334.3934

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Ana Luebbers
Sent: Thursday, March 22, 2007 1:58 PM
To: [hidden email]
Subject: Re: find command

Hi,

This seems to be a well known problem to SPSS.  This is what I found on
the SPSS website:

************************
" Problem Description:  I'm using SPSS 14.0 for Windows and I'm having
trouble with the Find/Replace function in the Syntax Editor. The one
thing that I can consistently replicate is that a string that starts in
the first character of the first line of a syntax file seems to be
invisible to the Find dialog box.

For instance, I have a syntax file that starts with a GET FILE command
in that first position, first line -- and that's the only appearance of
"get" in the file. If I try to Find (i.e., Edit->Find, or clicking the
Binoculars in the toolbar) the string "get" the application responds
with a "Not Found" message. If I then put either a single extra space or
an extra line before the GET FILE command, then the Find function can
find the "get" string. This problem is replicable -- it happens every
single time, in 14.0.1 as well.

What's wrong?

Resolution Subject: This appears to be a defect

Resolution Description:
This is a defect and has been fixed in SPSS 15.0 for Windows. As a
workaround if you have extended Find/Replaces to do as part of your
editing, you could select all the text in your syntax file, and
copy/paste to a simple text editor, such as NotePad or Programmer's File
Editor. Perform your editing and then copy/paste your edited code back
to the SPSS Syntax Editor and save.

There have been other reported issues with the Find feature in SPSS 14.0
and 15.0 that we have not been able to consistently replicate, and thus
cannot be certain have been fixed along with the other replicable
issues. One example involves a file with over 50 instances of a
particular string, but sporadically the Find/Replace dialog can only
"see" the first instance -- clicking Find Next in the dialog box does
not advance the cursor to the next instance. So far SPSS Development has
not been able to replicate this issue in a controlled environment, but
user reports indicate that closing and re-opening the syntax file does
seem to "re-set" the Find dialog's functionality. SPSS 16.0 for Windows
will use a new syntax editor and all Find/Replace functionality should
work as expected in that release.

We apologize for the inconvenience. "

*******************************


Ana

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
peter link
Sent: Thursday, March 22, 2007 2:43 PM
To: [hidden email]
Subject: Re: find command

Graham -

I've experienced what you describe and I use SPSS v11.5.  It can be very
frustrating.  My solution has been exactly what you seem to be doing,
which
is far from ideal.  I am not sure what causes the problem or what the
fix
is, but I would be very interested.

Peter

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]]On Behalf Of
Graham Wright
Sent: Thursday, March 22, 2007 11:21 AM
To: [hidden email]
Subject: find command


Something seems to be going wrong with the find command in my version of
SPSS 14. There are times when I am searching for a certain word in a
long sytnax file and I know it's there but the find command can't seem
to find it. I can even copy a word like "freq" that's in the file a
hundred times and past it into the find window and SPSS still tells me
that it never appears in the file. This seems to only happen after I've
searched for other commands first. Like I will search for "merge" and
the find command will work find, but then it won't be able to find
anything. I have to shut SPSS down and restart it to get the find
function to work again.  I've made sure I'm searching to correct file,
that my cursor is at the top of the syntax file before I start
searching, that I'm not highlighting any text etc and it still does it,
even if I have my cursor on the exact word I'm searching for. I have to
past the entire syntax file into text pad to find anything now. Does any
one know why this is happening?

-Graham
Reply | Threaded
Open this post in threaded view
|

Re: question about number of years applied

Peck, Jon
In reply to this post by Miriam L. Gerver
A colleague reminded me that the solution below is more complicated than needed.  This revised code takes advantage of the ability to form a set from a sequence of values.  The variable case is a sequence of values of the selected variables, so the code can simply pass this to set and take the size of the set in one step.

And while we're on the topic of sets, I want to point out that Python has all the set operators you learned about in school: union, intersections, difference, symmetric difference ...

-Jon Peck

begin program.
import spss, spssaux, spssdata

vardict = spssaux.VariableDict()
curs = spssdata.Spssdata(accessType='w', indexes=vardict.range("grant1yr", "grant5yr"))
curs.append(spssdata.vdef("YearsApplied", vlabel="Number of years with grant app", vfmt=("F",5,0)))
curs.commitdict()

for case in curs:
    curs.casevalues([len(set(case))])
curs.CClose()
end program.

-----Original Message-----
From: Peck, Jon
Sent: Thursday, March 22, 2007 2:48 PM
To: 'Richard Ristow'; [hidden email]
Subject: RE: Re: [SPSSX-L] question about number of years applied

Ok.  Here's the Python way using SPSS 15 (you knew that was coming, right?).  Others have posted pure SPSS ways of solving this problem that work, but the code below illustrates a general solution to counting distinct values in a group of variables without any extra assumptions.

Explanation after the code.

begin program.
import spss, spssaux, spssdata

vardict = spssaux.VariableDict()
curs = spssdata.Spssdata(accessType='w', indexes=vardict.range("Grant1yr", "Grant5yr"))
curs.append(spssdata.vdef("YearsApplied", vlabel="Number of years with grant app", vfmt=("F",5,0)))
curs.commitdict()

for case in curs:
    yrs = set([])
    for v in case:
        yrs.add(v)
    curs.casevalues([len(yrs)])
curs.CClose()
end program.

Explanation:

vardict... creates a dictionary in Python of the SPSS variables.
curs = ...  gets a write cursor to the data and defines grant1yr TO grant5yr as the variables to retrieve making use of the range method of VariableDict objects.
curs.append ... defines a new numeric variable and assigns a variable label and format.

for case in curs ... loops over the data.
yrs... makes use of the built-in set feature of Python, starting with an empty set for each case.
for v ... adds each value of the SPSS variables retrieved to the set.  It is the  nature of sets that the elements are all distinct.
So the curs.casevalues... line stores the size of the set, i.e., the number of distinct values in the new variable.

-Jon Peck
SPSS


-----Original Message-----
Dept. #        1st grant     2nd grant     3rd grant     4th grant     5th grant
1                   2001          2001           2002             2002
2                   2001          2002           2002             2002
3                   2001          2001           2002             2002
         2003
4                   2002          2003           2004             2005
5                   2004          2004           2005             2005
6                   2001          2002           2003             2004
         2005
7                   2003          2004           2004             2004

I'd like to generate a variable that indicates the number of years that a department has applied for grants. For example, Dept. #1 has applied for 4 grants, but has only applied in 2 year, Dept. 4 has applied for 4 grants, but has applied in 4 years.
Reply | Threaded
Open this post in threaded view
|

Re: find command

Graham Wright-2
In reply to this post by Butler, Deborah {FLNA}
Yeah, and I use SPSS through an academic site license, so upgrading is
not option.

This seems to be yet another thing that Stata and SAS can do just fine
on their own but I have to pay extra to get SPSS to do (like give
correct ci's for complex weighting schemes)....

-Graham





Butler, Deborah {FLNA} wrote:

> Man, anyone else find that frustrating?!
>
> I have a problem being told to buy an upgrade in order to fix a 'bug' on
> a feature which SHOULD have been working to begin with.  I think we
> should at least get a patch for it.  I certainly don't mind upgrading in
> order to get a new FEATURE, but not an existing one fixed.
>
>
> Debbie Butler
> SCP Modeling Analyst
> Frito Lay Operations
> 972.334.3934
>
> -----Original Message-----
> From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
> Ana Luebbers
> Sent: Thursday, March 22, 2007 1:58 PM
> To: [hidden email]
> Subject: Re: find command
>
> Hi,
>
> This seems to be a well known problem to SPSS.  This is what I found on
> the SPSS website:
>
> ************************
> " Problem Description:  I'm using SPSS 14.0 for Windows and I'm having
> trouble with the Find/Replace function in the Syntax Editor. The one
> thing that I can consistently replicate is that a string that starts in
> the first character of the first line of a syntax file seems to be
> invisible to the Find dialog box.
>
> For instance, I have a syntax file that starts with a GET FILE command
> in that first position, first line -- and that's the only appearance of
> "get" in the file. If I try to Find (i.e., Edit->Find, or clicking the
> Binoculars in the toolbar) the string "get" the application responds
> with a "Not Found" message. If I then put either a single extra space or
> an extra line before the GET FILE command, then the Find function can
> find the "get" string. This problem is replicable -- it happens every
> single time, in 14.0.1 as well.
>
> What's wrong?
>
> Resolution Subject: This appears to be a defect
>
> Resolution Description:
> This is a defect and has been fixed in SPSS 15.0 for Windows. As a
> workaround if you have extended Find/Replaces to do as part of your
> editing, you could select all the text in your syntax file, and
> copy/paste to a simple text editor, such as NotePad or Programmer's File
> Editor. Perform your editing and then copy/paste your edited code back
> to the SPSS Syntax Editor and save.
>
> There have been other reported issues with the Find feature in SPSS 14.0
> and 15.0 that we have not been able to consistently replicate, and thus
> cannot be certain have been fixed along with the other replicable
> issues. One example involves a file with over 50 instances of a
> particular string, but sporadically the Find/Replace dialog can only
> "see" the first instance -- clicking Find Next in the dialog box does
> not advance the cursor to the next instance. So far SPSS Development has
> not been able to replicate this issue in a controlled environment, but
> user reports indicate that closing and re-opening the syntax file does
> seem to "re-set" the Find dialog's functionality. SPSS 16.0 for Windows
> will use a new syntax editor and all Find/Replace functionality should
> work as expected in that release.
>
> We apologize for the inconvenience. "
>
> *******************************
>
>
> Ana
>
> -----Original Message-----
> From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
> peter link
> Sent: Thursday, March 22, 2007 2:43 PM
> To: [hidden email]
> Subject: Re: find command
>
> Graham -
>
> I've experienced what you describe and I use SPSS v11.5.  It can be very
> frustrating.  My solution has been exactly what you seem to be doing,
> which
> is far from ideal.  I am not sure what causes the problem or what the
> fix
> is, but I would be very interested.
>
> Peter
>
> -----Original Message-----
> From: SPSSX(r) Discussion [mailto:[hidden email]]On Behalf Of
> Graham Wright
> Sent: Thursday, March 22, 2007 11:21 AM
> To: [hidden email]
> Subject: find command
>
>
> Something seems to be going wrong with the find command in my version of
> SPSS 14. There are times when I am searching for a certain word in a
> long sytnax file and I know it's there but the find command can't seem
> to find it. I can even copy a word like "freq" that's in the file a
> hundred times and past it into the find window and SPSS still tells me
> that it never appears in the file. This seems to only happen after I've
> searched for other commands first. Like I will search for "merge" and
> the find command will work find, but then it won't be able to find
> anything. I have to shut SPSS down and restart it to get the find
> function to work again.  I've made sure I'm searching to correct file,
> that my cursor is at the top of the syntax file before I start
> searching, that I'm not highlighting any text etc and it still does it,
> even if I have my cursor on the exact word I'm searching for. I have to
> past the entire syntax file into text pad to find anything now. Does any
> one know why this is happening?
>
> -Graham
>