syntax to show a string variable under a certain number of characters

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

syntax to show a string variable under a certain number of characters

liradorovict
I have some zipcode data and a large data set, I use the first number of the zipcode to code for large regions. I noticed, however, that there are a few mistyped zipcodes under 5 characters and want to know if there is any way to flag them so I can recode them as missing. My variable is a string to account for the "-" displayed in full zipcodes (for example: 10016-0136). Is there any way to do this?

Thanks!
Reply | Threaded
Open this post in threaded view
|

Re: syntax to show a string variable under a certain number of characters

Maguin, Eugene
I think what you want is this:

If (index(zipcode,'-') ne 6) error=1.

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of liradorovict
Sent: Thursday, December 13, 2012 10:35 AM
To: [hidden email]
Subject: syntax to show a string variable under a certain number of characters

I have some zipcode data and a large data set, I use the first number of the zipcode to code for large regions. I noticed, however, that there are a few mistyped zipcodes under 5 characters and want to know if there is any way to flag them so I can recode them as missing. My variable is a string to account for the "-" displayed in full zipcodes (for example: 10016-0136). Is there any way to do this?

Thanks!




--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/syntax-to-show-a-string-variable-under-a-certain-number-of-characters-tp5716927.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: syntax to show a string variable under a certain number of characters

Bruce Weaver
Administrator
In reply to this post by liradorovict
In the FM (aka the Command Syntax Reference manual), see Universals > Transformation Expressions > String Functions.  Notice the CHAR.LENGTH function, which you can use to return the length of your string variable (with trailing blanks removed).


liradorovict wrote
I have some zipcode data and a large data set, I use the first number of the zipcode to code for large regions. I noticed, however, that there are a few mistyped zipcodes under 5 characters and want to know if there is any way to flag them so I can recode them as missing. My variable is a string to account for the "-" displayed in full zipcodes (for example: 10016-0136). Is there any way to do this?

Thanks!
--
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/).
Reply | Threaded
Open this post in threaded view
|

Re: syntax to show a string variable under a certain number of characters

Art Kendall
In reply to this post by liradorovict
Are you sure none of your zipcodes would be in New England?

see if this is what you want to do.

data list list/zip9(a10).
begin data
   "2909-1234"
   " 2909-1234"
   "20782-1438"
   "909-1234"
end data.
string cleanzip9 (a10).
compute wheredash = index(zip9,'-').
do if wheredash eq 6.
   do if wheredash eq 6 and substr(zip9,1,1) eq ' '.
      compute cleanzip9 = concat('0',ltrim((rtrim(zip9)))).
   ELSE.
      compute cleanzip9 = zip9.
   end if.
else if wheredash eq 5.
   compute cleanzip9 = concat('0',rtrim(zip9)).
ELSE.
   print / 'pattern not provided for' $casenum, ' ', zip9.
end if.
list.

Art Kendall
Social Research Consultants
On 12/13/2012 10:34 AM, liradorovict wrote:
I have some zipcode data and a large data set, I use the first number of the
zipcode to code for large regions. I noticed, however, that there are a few
mistyped zipcodes under 5 characters and want to know if there is any way to
flag them so I can recode them as missing. My variable is a string to
account for the "-" displayed in full zipcodes (for example: 10016-0136). Is
there any way to do this?

Thanks!




--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/syntax-to-show-a-string-variable-under-a-certain-number-of-characters-tp5716927.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
Art Kendall
Social Research Consultants
Reply | Threaded
Open this post in threaded view
|

Re: syntax to show a string variable under a certain number of characters

Art Kendall
Are you sure none of your zipcodes would be in New England?

see if this is what you want to do.

data list list/zip9(a10).
begin data
   "2909-1234"
   " 2909-1234"
   "20782-1438"
   "909-1234"
end data.
string cleanzip9 (a10).
compute wheredash = index(zip9,'-').
do if wheredash eq 6.
   do if wheredash eq 6 and substr(zip9,1,1) eq ' '.
      compute cleanzip9 = concat('0',ltrim((rtrim(zip9)))).
   ELSE.
      compute cleanzip9 = zip9.
   end if.
else if wheredash eq 5.
   compute cleanzip9 = concat('0',rtrim(zip9)).
ELSE.
   print / 'pattern not provided for' $casenum, ' ', zip9.
end if.
list.
Art Kendall
Social Research Consultants
On 12/13/2012 11:55 AM, Art Kendall wrote:
Are you sure none of your zipcodes would be in New England?

see if this is what you want to do.

data list list/zip9(a10).
begin data
   "2909-1234"
   " 2909-1234"
   "20782-1438"
   "909-1234"
end data.
string cleanzip9 (a10).
compute wheredash = index(zip9,'-').
do if wheredash eq 6.
   do if wheredash eq 6 and substr(zip9,1,1) eq ' '.
      compute cleanzip9 = concat('0',ltrim((rtrim(zip9)))).
   ELSE.
      compute cleanzip9 = zip9.
   end if.
else if wheredash eq 5.
   compute cleanzip9 = concat('0',rtrim(zip9)).
ELSE.
   print / 'pattern not provided for' $casenum, ' ', zip9.
end if.
list.

Art Kendall
Social Research Consultants
On 12/13/2012 10:34 AM, liradorovict wrote:
I have some zipcode data and a large data set, I use the first number of the
zipcode to code for large regions. I noticed, however, that there are a few
mistyped zipcodes under 5 characters and want to know if there is any way to
flag them so I can recode them as missing. My variable is a string to
account for the "-" displayed in full zipcodes (for example: 10016-0136). Is
there any way to do this?

Thanks!




--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/syntax-to-show-a-string-variable-under-a-certain-number-of-characters-tp5716927.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
Art Kendall
Social Research Consultants
Reply | Threaded
Open this post in threaded view
|

Re: syntax to show a string variable under a certain number of characters

Rick Oliver-3
If zip code is a string, then I think it would normally contain the leading zeroes, but assuming it does not:

data list list/zip9(a10).
begin data
   "2909-1234"
   " 2909-1234"
   "20782-1438"
   "909-1234"
end data.
compute zip9=replace(zip9, "-", "").
compute zip9=string(number(ltrim(zip9), n9), n9).
compute zip9=concat(substr(zip9, 1, 4), "-", substr(zip9, 5)).
list.

Rick Oliver
Senior Information Developer
IBM Business Analytics (SPSS)
E-mail: [hidden email]




From:        Art Kendall <[hidden email]>
To:        [hidden email],
Date:        12/13/2012 11:04 AM
Subject:        Re: syntax to show a string variable under a certain number of              characters
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




Are you sure none of your zipcodes would be in New England?

see if this is what you want to do.

data list list/zip9(a10).
begin data
  "2909-1234"
  " 2909-1234"
  "20782-1438"
  "909-1234"
end data.
string cleanzip9 (a10).
compute wheredash = index(zip9,'-').
do if wheredash eq 6.
  do if wheredash eq 6 and substr(zip9,1,1) eq ' '.
     compute cleanzip9 = concat('0',ltrim((rtrim(zip9)))).
  ELSE.
     compute cleanzip9 = zip9.
  end if.
else if wheredash eq 5.
  compute cleanzip9 = concat('0',rtrim(zip9)).
ELSE.
  print / 'pattern not provided for' $casenum, ' ', zip9.
end if.
list.

Art Kendall
Social Research Consultants

On 12/13/2012 11:55 AM, Art Kendall wrote:
Are you sure none of your zipcodes would be in New England?

see if this is what you want to do.

data list list/zip9(a10).
begin data
  "2909-1234"
  " 2909-1234"
  "20782-1438"
  "909-1234"
end data.
string cleanzip9 (a10).
compute wheredash = index(zip9,'-').
do if wheredash eq 6.
  do if wheredash eq 6 and substr(zip9,1,1) eq ' '.
     compute cleanzip9 = concat('0',ltrim((rtrim(zip9)))).
  ELSE.
     compute cleanzip9 = zip9.
  end if.
else if wheredash eq 5.
  compute cleanzip9 = concat('0',rtrim(zip9)).
ELSE.
  print / 'pattern not provided for' $casenum, ' ', zip9.
end if.
list.


Art Kendall
Social Research Consultants

On 12/13/2012 10:34 AM, liradorovict wrote:
I have some zipcode data and a large data set, I use the first number of the
zipcode to code for large regions. I noticed, however, that there are a few
mistyped zipcodes under 5 characters and want to know if there is any way to
flag them so I can recode them as missing. My variable is a string to
account for the "-" displayed in full zipcodes (for example: 10016-0136). Is
there any way to do this?

Thanks!




--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/syntax-to-show-a-string-variable-under-a-certain-number-of-characters-tp5716927.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to
LISTSERV@... (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: syntax to show a string variable under a certain number of characters

Melissa Ives
In reply to this post by liradorovict
Look at  CHAR.LENGTH(strexpr). Numeric. Returns the length of strexpr in characters, with any trailing blanks removed.
Compute a flag if it is <5.
M
-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of liradorovict
Sent: Thursday, December 13, 2012 9:35 AM
To: [hidden email]
Subject: [SPSSX-L] syntax to show a string variable under a certain number of characters

I have some zipcode data and a large data set, I use the first number of the zipcode to code for large regions. I noticed, however, that there are a few mistyped zipcodes under 5 characters and want to know if there is any way to flag them so I can recode them as missing. My variable is a string to account for the "-" displayed in full zipcodes (for example: 10016-0136). Is there any way to do this?

Thanks!




--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/syntax-to-show-a-string-variable-under-a-certain-number-of-characters-tp5716927.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

PRIVILEGED AND CONFIDENTIAL INFORMATION
This transmittal and any attachments may contain PRIVILEGED AND
CONFIDENTIAL information and is intended only for the use of the
addressee. If you are not the designated recipient, or an employee
or agent authorized to deliver such transmittals to the designated
recipient, you are hereby notified that any dissemination,
copying or publication of this transmittal is strictly prohibited. If
you have received this transmittal in error, please notify us
immediately by replying to the sender and delete this copy from your
system. You may also call us at (309) 827-6026 for assistance.

=====================
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: syntax to show a string variable under a certain number of characters

Rick Oliver-3
In reply to this post by Art Kendall
Oops. I put the dash in the wrong place:

data list list/zip9(a10).
begin data
   "2909-1234"
   " 2909-1234"
   "20782-1438"
   "909-1234"
end data.
compute zip9=replace(zip9, "-", "").
compute zip9=string(number(ltrim(zip9), n9), n9).
compute zip9=concat(substr(zip9, 1, 5), "-", substr(zip9, 6)).
list.


Rick Oliver
Senior Information Developer
IBM Business Analytics (SPSS)
E-mail: [hidden email]




From:        Rick Oliver/Chicago/IBM
To:        [hidden email],
Cc:        [hidden email]
Date:        12/13/2012 11:18 AM
Subject:        Re: syntax to show a string variable under a certain number of              characters



If zip code is a string, then I think it would normally contain the leading zeroes, but assuming it does not:

data list list/zip9(a10).
begin data
   "2909-1234"
   " 2909-1234"
   "20782-1438"
   "909-1234"
end data.
compute zip9=replace(zip9, "-", "").
compute zip9=string(number(ltrim(zip9), n9), n9).
compute zip9=concat(substr(zip9, 1, 4), "-", substr(zip9, 5)).
list.

Rick Oliver
Senior Information Developer
IBM Business Analytics (SPSS)
E-mail: [hidden email]





From:        Art Kendall <[hidden email]>
To:        [hidden email],
Date:        12/13/2012 11:04 AM
Subject:        Re: syntax to show a string variable under a certain number of              characters
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




Are you sure none of your zipcodes would be in New England?

see if this is what you want to do.

data list list/zip9(a10).
begin data
  "2909-1234"
  " 2909-1234"
  "20782-1438"
  "909-1234"
end data.
string cleanzip9 (a10).
compute wheredash = index(zip9,'-').
do if wheredash eq 6.
  do if wheredash eq 6 and substr(zip9,1,1) eq ' '.
     compute cleanzip9 = concat('0',ltrim((rtrim(zip9)))).
  ELSE.
     compute cleanzip9 = zip9.
  end if.
else if wheredash eq 5.
  compute cleanzip9 = concat('0',rtrim(zip9)).
ELSE.
  print / 'pattern not provided for' $casenum, ' ', zip9.
end if.
list.

Art Kendall
Social Research Consultants

On 12/13/2012 11:55 AM, Art Kendall wrote:
Are you sure none of your zipcodes would be in New England?

see if this is what you want to do.

data list list/zip9(a10).
begin data
  "2909-1234"
  " 2909-1234"
  "20782-1438"
  "909-1234"
end data.
string cleanzip9 (a10).
compute wheredash = index(zip9,'-').
do if wheredash eq 6.
  do if wheredash eq 6 and substr(zip9,1,1) eq ' '.
     compute cleanzip9 = concat('0',ltrim((rtrim(zip9)))).
  ELSE.
     compute cleanzip9 = zip9.
  end if.
else if wheredash eq 5.
  compute cleanzip9 = concat('0',rtrim(zip9)).
ELSE.
  print / 'pattern not provided for' $casenum, ' ', zip9.
end if.
list.


Art Kendall
Social Research Consultants

On 12/13/2012 10:34 AM, liradorovict wrote:
I have some zipcode data and a large data set, I use the first number of the
zipcode to code for large regions. I noticed, however, that there are a few
mistyped zipcodes under 5 characters and want to know if there is any way to
flag them so I can recode them as missing. My variable is a string to
account for the "-" displayed in full zipcodes (for example: 10016-0136). Is
there any way to do this?

Thanks!




--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/syntax-to-show-a-string-variable-under-a-certain-number-of-characters-tp5716927.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to
LISTSERV@... (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: syntax to show a string variable under a certain number of characters

liradorovict
In reply to this post by liradorovict
Thanks everyone, your suggestions were very helpful.
Reply | Threaded
Open this post in threaded view
|

Re: syntax to show a string variable under a certain number of characters

Art Kendall
People dealing with addresses might find this useful

http://www.melissadata.com/lookups/batchaddresscheck.asp
Art Kendall
Social Research Consultants
On 12/13/2012 3:57 PM, liradorovict wrote:
Thanks everyone, your suggestions were *very* helpful.



--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/syntax-to-show-a-string-variable-under-a-certain-number-of-characters-tp5716927p5716936.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
Art Kendall
Social Research Consultants