Hi all,
I have a variable with multiple and differing string responses separated by a comma E.g. V1 Event1, Event2, Event3 Event5 Event10, Event4, Event1, Event7 Event3, Event7 Is there a way of creating a variable based on whether an Event is present in V1 or not - (e.g. if V1 contains Event3, then V2=1; if not, V2=0) The order of the strings is random, such that the target may appear in any position in v1 Each "Event" is a string of differing characters lengths Thanks! |
For checking if one string exists you can use the INDEX function. INDEX returns 0 if the substring does not exist within the target string (otherwise it returns the character location the substring starts). So if [String] is the multiple response variable, the below code will do what you want.
**********. COMPUTE V1 = ( INDEX(String,"Event1" ) > 0). **********. You can do this for multiple strings using DO REPEAT. **********. DATA LIST FREE / String (A20). BEGIN DATA Event1, Event2, Event3 Event5 Event10, Event4, Event1, Event7 Event3, Event7 END DATA. *For multiple strings. VECTOR V(10, F1.0). DO REPEAT V = V1 TO V10 /a = "Event1" "Event2" "Event3" "Event4" "Event5" "Event6" "Event7" "Event8" "Event9" "Event10". COMPUTE V = ( INDEX(String,a) > 0 ). END REPEAT. EXECUTE. **********. |
In reply to this post by SophieR
if these are the stings you actually mean
then something like the syntax below will work. Alternative approaches are possible depending on the details of what you have and what you want to do. e.g., If you can re-read the data as multiple response data (one event per variable) You can do an AUTORECODE with the /group option. recode various s peelings, casings together if necessary and us MULT RESPONSE or CTABLES to deal with the set of multiple response variables. data list list /v1 (a40). begin data "Event1, Event2, Event3" "event10,eveNt8,EVent9" "Event5" "Event10, Event4, Event1, Event7" "Event3, Event7" end data. *if your data may have variations in casing, you would want to change it to a consistent case. string upper_v1 (a40). compute upper_V1 = CONCAT(rtrim(upcase(V1)),","). numeric hasEvent1 to hasEvent10 (f1). compute hasEvent1 = index(upper_V1,"EVENT1,") gt 0. compute hasEvent2 = index(upper_V1,"EVENT2,") gt 0. compute hasEvent3 = index(upper_V1,"EVENT3,") gt 0. compute hasEvent4 = index(upper_V1,"EVENT4,") gt 0. compute hasEvent5 = index(upper_V1,"EVENT5,") gt 0. compute hasEvent6 = index(upper_V1,"EVENT6,") gt 0. compute hasEvent7 = index(upper_V1,"EVENT7,") gt 0. compute hasEvent8 = index(upper_V1,"EVENT8,") gt 0. compute hasEvent9 = index(upper_V1,"EVENT9,") gt 0. compute hasEvent10 = index(upper_V1,"EVENT10,")gt 0. list.
Art Kendall
Social Research Consultants |
Administrator
|
In reply to this post by SophieR
Rather inconvenient data structure!
Particularly if any string response is a substring of another. You would do well to split this beast into multiple variables! A topic covered in excruciating detail in the past (search this group for PARSE). --
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?" |
Administrator
|
Using Art's sample data, consider adapting the following.
data list list /v1 (a40). begin data "Event1, Event2, Event3" "event10,eveNt8,EVent9" "Event5" "Event10, Event4, Event1, Event7" "Event3, Event7" end data. DATASET NAME raw. COMPUTE @ID@=$CASENUM. WRITE OUTFILE "C:\Windows\Temp\junk.txt" / @ID@ ," ", v1 . EXECUTE. PRESERVE. SET RESULTS=NONE ERRORS=NONE. DATA LIST LIST FILE "C:\Windows\Temp\junk.txt"/ @ID@ (F4) X01 TO X10 (10A10). EXECUTE. RESTORE. MATCH FILES /FILE raw / FILE * / BY @ID@. LIST. ERASE FILE "C:\Windows\Temp\junk.txt" .
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?" |
Free forum by Nabble | Edit this page |