Capture variable names as text

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

Capture variable names as text

William Dudley
All thanks in advance for any ideas.

I have  a long list of binary variables (~300).  These variables indicate if a patient received a given drug (1 = yes 0 = no).  I want to operated on these variables to arrive a a string indicating which drug combination the patient is receiving.  I have tried  do repeat with an expanded version of this (expanded to include 300 X variables and sting vaiues for Y.  I still have to work out logic for concatenating the DrugstrX variables.

String Drugstr1 (A10).
String Drugstr2 (A10).

Compute Drugstr1 = "".
Compute Drugstr2 = "".

Do repeat x = Aspiring ibuprofen 
/y = "Aspirin" "Ibuprofen".
if x = 1 and Drugstr1 = "" Drugstr1 = y.
if x = 1 and Drugstr1 NE "" Drugstr2 = y.

​End Repeat.

Then I would concatenate the two Drugstr variables into a new string variable.
 BUT I get  this message

>Error # 4511 in column 1024.  Text: (End of Command)
>Undefined error #4511 - not found in file "C:\PROGRA~1\IBM\SPSS\STATIS~1\24\lang\en\spss.err"
>Execution of this command stops.

​Perhaps this is because my variable lists are so long (I have about 300 drugs)???


​So I wonder if it is possible to simply capture  the variable name as text​
Is there a function (say vartext) in spss syntax like...


String1 (A30).
IF Var1 = 1 String1 = vartex(var1).

Thanks
Bill





--
William N Dudley, PhD
President
Piedmont Research Strategies, Inc
===================== 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
Reply | Threaded
Open this post in threaded view
|

Re: Capture variable names as text

Maguin, Eugene

Can I assume you have considered/tried and rejected this. I’m assuming a wide format structured as id d1 to d300.

Then:

String AllDrugs(a300).

Vector dd=d1 to d300.

Loop #i=1 to 300.

+  compute #d=dd(#i).  /* not sure if a subscripted var will work in these functions.

+  compute alldrugs=concat(rtrim(alldrugs),string(#d,f1.0)).

End loop.

 

Gene Maguin

 

 

 

From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of William Dudley
Sent: Wednesday, August 9, 2017 8:05 AM
To: [hidden email]
Subject: Capture variable names as text

 

All thanks in advance for any ideas.

 

I have  a long list of binary variables (~300).  These variables indicate if a patient received a given drug (1 = yes 0 = no).  I want to operated on these variables to arrive a a string indicating which drug combination the patient is receiving.  I have tried  do repeat with an expanded version of this (expanded to include 300 X variables and sting vaiues for Y.  I still have to work out logic for concatenating the DrugstrX variables.

 

String Drugstr1 (A10).

String Drugstr2 (A10).

 

Compute Drugstr1 = "".

Compute Drugstr2 = "".

 

Do repeat x = Aspiring ibuprofen 

/y = "Aspirin" "Ibuprofen".

if x = 1 and Drugstr1 = "" Drugstr1 = y.

if x = 1 and Drugstr1 NE "" Drugstr2 = y.

 

End Repeat.

 

Then I would concatenate the two Drugstr variables into a new string variable.

 BUT I get  this message

 

>Error # 4511 in column 1024.  Text: (End of Command)

>Undefined error #4511 - not found in file "C:\PROGRA~1\IBM\SPSS\STATIS~1\24\lang\en\spss.err"

>Execution of this command stops.

 

​Perhaps this is because my variable lists are so long (I have about 300 drugs)???

 

 

​So I wonder if it is possible to simply capture  the variable name as text​

Is there a function (say vartext) in spss syntax like...

 

 

String1 (A30).

IF Var1 = 1 String1 = vartex(var1).

 

Thanks

Bill

 

 

 

 

 

--

William N Dudley, PhD

President

Piedmont Research Strategies, Inc

===================== 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

===================== 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
Reply | Threaded
Open this post in threaded view
|

Re: Capture variable names as text

David Marso
Administrator
Gene,
I read William's request as a desire to concatenate the actual variable names into a long string if the respective value is 1.
Here is my admittedly clumsy solution.
---
DATASET CLOSE ALL.
DATA LIST / x01 TO x10 (10F1.0).
BEGIN DATA
1100110010
1001010100
1100110001
1101001001
0001010011
1010001001
0001001001
END DATA.
DATASET NAME raw.
DATASET COPY copyraw.
DATASET ACTIVATE copyraw.

SELECT IF $CASENUM=1.
/* FLIP will put the original variable names into a string variable called CASE_LBL */.
FLIP.
MATCH FILES /FILE */RENAME CASE_LBL=name / KEEP name.

/* Now put them all into a single row and match to original data */.
CASESTOVARS.
STRING @2 (A1).
COMPUTE @2="".
COMPUTE merge=1.
DATASET NAME varnames.
DATASET ACTIVATE raw.
COMPUTE @1=0.
COMPUTE merge=1.
MATCH FILES /FILE */TABLE varnames /BY merge.

STRING alldrugs (A1000).
DO REPEAT x= x01 TO x10 @1/name=name.1 TO name.10 @2.
IF (x EQ 1) alldrugs=CONCAT(RTRIM(alldrugs)," ",name).
END REPEAT.

/* Clean up */.
ALTER TYPE alldrugs(AMIN).
LIST x01 TO x10 alldrugs .
DELETE VARIABLES @1 merge name.1 TO @2.
DATASET CLOSE copyraw.
DATASET CLOSE  varnames.
 
x01 x02 x03 x04 x05 x06 x07 x08 x09 x10 alldrugs
 
 1   1   0   0   1   1   0   0   1   0   x01 x02 x05 x06 x09
 1   0   0   1   0   1   0   1   0   0   x01 x04 x06 x08
 1   1   0   0   1   1   0   0   0   1   x01 x02 x05 x06 x10
 1   1   0   1   0   0   1   0   0   1   x01 x02 x04 x07 x10
 0   0   0   1   0   1   0   0   1   1   x04 x06 x09 x10
 1   0   1   0   0   0   1   0   0   1   x01 x03 x07 x10
 0   0   0   1   0   0   1   0   0   1   x04 x07 x10
 
 
Number of cases read:  7    Number of cases listed:  7




Maguin, Eugene wrote
Can I assume you have considered/tried and rejected this. I’m assuming a wide format structured as id d1 to d300.
Then:
String AllDrugs(a300).
Vector dd=d1 to d300.
Loop #i=1 to 300.
+  compute #d=dd(#i).  /* not sure if a subscripted var will work in these functions.
+  compute alldrugs=concat(rtrim(alldrugs),string(#d,f1.0)).
End loop.

Gene Maguin



From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of William Dudley
Sent: Wednesday, August 9, 2017 8:05 AM
To: [hidden email]
Subject: Capture variable names as text

All thanks in advance for any ideas.

I have  a long list of binary variables (~300).  These variables indicate if a patient received a given drug (1 = yes 0 = no).  I want to operated on these variables to arrive a a string indicating which drug combination the patient is receiving.  I have tried  do repeat with an expanded version of this (expanded to include 300 X variables and sting vaiues for Y.  I still have to work out logic for concatenating the DrugstrX variables.

String Drugstr1 (A10).
String Drugstr2 (A10).

Compute Drugstr1 = "".
Compute Drugstr2 = "".

Do repeat x = Aspiring ibuprofen
/y = "Aspirin" "Ibuprofen".
if x = 1 and Drugstr1 = "" Drugstr1 = y.
if x = 1 and Drugstr1 NE "" Drugstr2 = y.

​End Repeat.

Then I would concatenate the two Drugstr variables into a new string variable.
 BUT I get  this message

>Error # 4511 in column 1024.  Text: (End of Command)
>Undefined error #4511 - not found in file "C:\PROGRA~1\IBM\SPSS\STATIS~1\24\lang\en\spss.err"
>Execution of this command stops.

​Perhaps this is because my variable lists are so long (I have about 300 drugs)???


​So I wonder if it is possible to simply capture  the variable name as text​
Is there a function (say vartext) in spss syntax like...


String1 (A30).
IF Var1 = 1 String1 = vartex(var1).

Thanks
Bill





--
William N Dudley, PhD
President
Piedmont Research Strategies, Inc
===================== To manage your subscription to SPSSX-L, send a message to [hidden email]<mailto:[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

=====================
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
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Capture variable names as text

Maguin, Eugene
Yeah, your reading is right; mine was wrong. I ran your code. Very nice.
Gene Maguin

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of David Marso
Sent: Monday, August 14, 2017 6:23 PM
To: [hidden email]
Subject: Re: Capture variable names as text

Gene,
I read William's request as a desire to concatenate the actual variable names into a long string if the respective value is 1.
Here is my admittedly clumsy solution.
---
DATASET CLOSE ALL.
DATA LIST / x01 TO x10 (10F1.0).
BEGIN DATA
1100110010
1001010100
1100110001
1101001001
0001010011
1010001001
0001001001
END DATA.
DATASET NAME raw.
DATASET COPY copyraw.
DATASET ACTIVATE copyraw.

SELECT IF $CASENUM=1.
/* FLIP will put the original variable names into a string variable called CASE_LBL */.
FLIP.
MATCH FILES /FILE */RENAME CASE_LBL=name / KEEP name.

/* Now put them all into a single row and match to original data */.
CASESTOVARS.
STRING @2 (A1).
COMPUTE @2="".
COMPUTE merge=1.
DATASET NAME varnames.
DATASET ACTIVATE raw.
COMPUTE @1=0.
COMPUTE merge=1.
MATCH FILES /FILE */TABLE varnames /BY merge.

STRING alldrugs (A1000).
DO REPEAT x= x01 TO x10 @1/name=name.1 TO name.10 @2.
IF (x EQ 1) alldrugs=CONCAT(RTRIM(alldrugs)," ",name).
END REPEAT.

/* Clean up */.
ALTER TYPE alldrugs(AMIN).
LIST x01 TO x10 alldrugs .
DELETE VARIABLES @1 merge name.1 TO @2.
DATASET CLOSE copyraw.
DATASET CLOSE  varnames.
 
x01 x02 x03 x04 x05 x06 x07 x08 x09 x10 alldrugs
 
 1   1   0   0   1   1   0   0   1   0   x01 x02 x05 x06 x09
 1   0   0   1   0   1   0   1   0   0   x01 x04 x06 x08
 1   1   0   0   1   1   0   0   0   1   x01 x02 x05 x06 x10
 1   1   0   1   0   0   1   0   0   1   x01 x02 x04 x07 x10
 0   0   0   1   0   1   0   0   1   1   x04 x06 x09 x10
 1   0   1   0   0   0   1   0   0   1   x01 x03 x07 x10
 0   0   0   1   0   0   1   0   0   1   x04 x07 x10
 
 
Number of cases read:  7    Number of cases listed:  7





Maguin, Eugene wrote

> Can I assume you have considered/tried and rejected this. I’m assuming
> a wide format structured as id d1 to d300.
> Then:
> String AllDrugs(a300).
> Vector dd=d1 to d300.
> Loop #i=1 to 300.
> +  compute #d=dd(#i).  /* not sure if a subscripted var will work in
> + these
> functions.
> +  compute alldrugs=concat(rtrim(alldrugs),string(#d,f1.0)).
> End loop.
>
> Gene Maguin
>
>
>
> From: SPSSX(r) Discussion [mailto:

> SPSSX-L@.UGA

> ] On Behalf Of William Dudley
> Sent: Wednesday, August 9, 2017 8:05 AM
> To:

> SPSSX-L@.UGA

> Subject: Capture variable names as text
>
> All thanks in advance for any ideas.
>
> I have  a long list of binary variables (~300).  These variables
> indicate if a patient received a given drug (1 = yes 0 = no).  I want
> to operated on these variables to arrive a a string indicating which
> drug combination the patient is receiving.  I have tried  do repeat
> with an expanded version of this (expanded to include 300 X variables
> and sting vaiues for Y.  I still have to work out logic for
> concatenating the DrugstrX variables.
>
> String Drugstr1 (A10).
> String Drugstr2 (A10).
>
> Compute Drugstr1 = "".
> Compute Drugstr2 = "".
>
> Do repeat x = Aspiring ibuprofen
> /y = "Aspirin" "Ibuprofen".
> if x = 1 and Drugstr1 = "" Drugstr1 = y.
> if x = 1 and Drugstr1 NE "" Drugstr2 = y.
>
> ​End Repeat.
>
> Then I would concatenate the two Drugstr variables into a new string
> variable.
>  BUT I get  this message
>
>>Error # 4511 in column 1024.  Text: (End of Command) Undefined error
>>#4511 - not found in file
"C:\PROGRA~1\IBM\SPSS\STATIS~1\24\lang\en\spss.err"

>>Execution of this command stops.
>
> ​Perhaps this is because my variable lists are so long (I have about
> 300 drugs)???
>
>
> ​So I wonder if it is possible to simply capture  the variable name as
> text​ Is there a function (say vartext) in spss syntax like...
>
>
> String1 (A30).
> IF Var1 = 1 String1 = vartex(var1).
>
> Thanks
> Bill
>
>
>
>
>
> --
> William N Dudley, PhD
> President
> Piedmont Research Strategies, Inc
> ===================== To manage your subscription to SPSSX-L, send a
> message to

> LISTSERV@.UGA

> &lt;mailto:

> LISTSERV@.UGA

> &gt; (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
>
> =====================
> To manage your subscription to SPSSX-L, send a message to

> LISTSERV@.UGA

>  (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





-----
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Capture-variable-names-as-text-tp5734654p5734656.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
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

=====================
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
Reply | Threaded
Open this post in threaded view
|

Re: Capture variable names as text

Jon Peck
In reply to this post by William Dudley
Here is a concise Python solution using the SPSSINC TRANS extension command normally installed with Statistics.

First, the code, and then the explanation.

begin program.
import spssaux

class getvars(object):
    """return string of variable names for case values equal to 1"""
    def __init__(self, first, last):
        self.varnames = spssaux.VariableDict().expand("%s to %s" % (first, last))

        def func(*allvars):
            nameset = [self.varnames[i] for i in range(len(allvars)) if allvars[i] ==1]
            return " ".join(nameset)
        self.func = func
end program.

spssinc trans result = codelist type=500
/initial "getvars('V2', 'V9')"
/variables V2 to V9
/formula "func(<>)".

The begin program block creates a function named func that will be evaluated for each case when called by spssinc trans.
In the spssinc trans command, result specifies the variable name to hold the list of names.  type is the length in bytes of that name.
The initial and variables subcommand specify the variables to be scanned - V2 to v9 in this example (case matters).  Both lists must imply the same variables.  Note that the variable names in the initial subcommand are in quotes different from the surrounding quotes.
The formula subcommand uses a special notation, <>, to refer to the variables defined in the initial subcommand.


On Wed, Aug 9, 2017 at 6:05 AM, William Dudley <[hidden email]> wrote:
All thanks in advance for any ideas.

I have  a long list of binary variables (~300).  These variables indicate if a patient received a given drug (1 = yes 0 = no).  I want to operated on these variables to arrive a a string indicating which drug combination the patient is receiving.  I have tried  do repeat with an expanded version of this (expanded to include 300 X variables and sting vaiues for Y.  I still have to work out logic for concatenating the DrugstrX variables.

String Drugstr1 (A10).
String Drugstr2 (A10).

Compute Drugstr1 = "".
Compute Drugstr2 = "".

Do repeat x = Aspiring ibuprofen 
/y = "Aspirin" "Ibuprofen".
if x = 1 and Drugstr1 = "" Drugstr1 = y.
if x = 1 and Drugstr1 NE "" Drugstr2 = y.

​End Repeat.

Then I would concatenate the two Drugstr variables into a new string variable.
 BUT I get  this message

>Error # 4511 in column 1024.  Text: (End of Command)
>Undefined error #4511 - not found in file "C:\PROGRA~1\IBM\SPSS\STATIS~1\24\lang\en\spss.err"
>Execution of this command stops.

​Perhaps this is because my variable lists are so long (I have about 300 drugs)???


​So I wonder if it is possible to simply capture  the variable name as text​
Is there a function (say vartext) in spss syntax like...


String1 (A30).
IF Var1 = 1 String1 = vartex(var1).

Thanks
Bill





--
William N Dudley, PhD
President
Piedmont Research Strategies, Inc
===================== 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



--
Jon K Peck
[hidden email]

===================== 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