Dear All,
I have a 5-digit identification variable for single cases. I need to select cases based on the last digit, which is either a 1, 2 or 3. Can anybody provide me with a syntax example how to program such syntax (e.g. using the select-command?) Many thanks! Tino |
Administrator
|
Is your ID variable numeric or string? Is it always 5 digits in length? (It's always good to provide examples of what you're dealing with.)
If it is string and always 5 digits in length, consider using the CHAR.SUBSTR function to extract the last digit and the NUMBER function to convert it to a numeric variable. Something like this (untested): COMPUTE LastDigit = NUMBER(CHAR.SUBSTR(IDvar,5,1),F1). If the length is not always 5, you'll have to make it a bit more complicated -- e.g., using the CHAR.LENGTH function to get the length of the string, etc. HTH.
--
Bruce Weaver bweaver@lakeheadu.ca http://sites.google.com/a/lakeheadu.ca/bweaver/ "When all else fails, RTFM." PLEASE NOTE THE FOLLOWING: 1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above. 2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/). |
Tino If your ID is numeric and all IDs have 5 digits, try (untested): Compute x = mod(id/1000). Count y = x (1,2,3) . Recode y (2,3 =1)(else =0) into z. Format x y z (f2.0). Var lab x 'Last digit of ID' /y 'IDs ending with 1, 2 or 3' /z '1 = select, 0 = Other'. Val lab y 1 'Ends with 1' 2 'Ends with 2 ' 3 'Ends with 3' 0 'Other' /z 'Ends in 1,2 or 3' 0 'Other'. Freq x y z. If some IDs have fewer than 5 digits you may need to use: Format id (n5). . . at the beginning. John F Hall (Mr) [Retired academic survey researcher] Email: [hidden email] Website: www.surveyresearch.weebly.com SPSS start page: www.surveyresearch.weebly.com/spss-without-tears.html -----Original Message----- Is your ID variable numeric or string? Is it always 5 digits in length? (It's always good to provide examples of what you're dealing with.) If it is string and always 5 digits in length, consider using the CHAR.SUBSTR function to extract the last digit and the NUMBER function to convert it to a numeric variable. Something like this (untested): COMPUTE LastDigit = NUMBER(CHAR.SUBSTR(IDvar,5,1),F1). If the length is not always 5, you'll have to make it a bit more complicated -- e.g., using the CHAR.LENGTH function to get the length of the string, etc. HTH. tino wrote > Dear All, > I have a 5-digit identification variable for single cases. I need to > select cases based on the last digit, which is either a 1, 2 or 3. Can > anybody provide me with a syntax example how to program such syntax (e.g. > using the select-command?) > > Many thanks! Tino ----- -- Bruce Weaver http://sites.google.com/a/lakeheadu.ca/bweaver/ "When all else fails, RTFM." NOTE: My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above. -- View this message in context: http://spssx-discussion.1045642.n5.nabble.com/select-cases-using-digits-tp5722034p5722035.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 |
If it's numeric, you can extract the last digit with MODULUS(id,10) (for MODULUS, see http://www.pythonforspss.org/modulus/). So for selecting all cases who's ID end with "2", try
select if mod(id,10) = 2. execute. HTH, Ruben |
Free forum by Nabble | Edit this page |