extracting parts of string variable

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

extracting parts of string variable

Paul Mcgeoghan
Hi,

I have a string variable which is IP address:
So we have 12.123.124.12 as an example.

I want to create 2 new variables:
one of which has: 12.123
another which is 12.123.124

How do I do this?
Thanks,
Paul

=====================
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: extracting parts of string variable

Oliver, Richard
If all values have the exact same format -- 2 digits, decimal, 3 digits, decimal, 3 digits, decimal... -- the solution is simple:

data list list /stringvar (a13).
begin data.
12.123.124.12
end data.
end data.
string newstring1 (a6) newstring2 (a10).
compute newstring1=stringvar.
compute newstring2=stringvar.
list.

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Paul McGeoghan
Sent: Thursday, May 21, 2009 8:48 AM
To: [hidden email]
Subject: extracting parts of string variable

Hi,

I have a string variable which is IP address:
So we have 12.123.124.12 as an example.

I want to create 2 new variables:
one of which has: 12.123
another which is 12.123.124

How do I do this?
Thanks,
Paul

=====================
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: extracting parts of string variable

Carlos Renato (www.estatistico.org)
In reply to this post by Paul Mcgeoghan
Another option is the command SUBSTR.

Carlos Renato

2009/5/21 Paul McGeoghan <[hidden email]>
Hi,

I have a string variable which is IP address:
So we have 12.123.124.12 as an example.

I want to create 2 new variables:
one of which has: 12.123
another which is 12.123.124

How do I do this?
Thanks,
Paul

=====================
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: extracting parts of string variable

Paul Mcgeoghan
In reply to this post by Paul Mcgeoghan
The IP addresses are of varying format:
xx.xxx.x.xx
xx.xx.x.xx
xx.xxx.xxx.xx
xxx.xxx.xx.xx

So really I need to find some way of finding the first full stop and then
anything to the left of that, store in new variable.
Then find the 2nd full stop and anything to the left of that, store in new
variable.

=====================
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: extracting parts of string variable

John McConnell-2
Hi Paul

Try this

Assuming your IP variable is called IP...

-----------------------------------

string ip1 ip2 ip3 ip4 (a3).
string stub1 stub2 (a15).

comp dotloc = index (ip,'.').
comp ip1=substr(ip,1,dotloc-1).
comp stub1=substr(ip,dotloc+1).

comp dotloc = index (stub1,'.').
comp ip2=substr(stub1,1,dotloc-1).
comp stub2=substr(stub1,dotloc+1).

comp dotloc = index (stub2,'.').
comp ip3=substr(stub2,1,dotloc-1).
comp ip4=substr(stub2,dotloc+1).

exe.

-----------------------------------

Thanks

John


John McConnell
analytical people
500 Chiswick High Road
London
W4 5RG, United Kingdom
www.analyticalpeople.com

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Paul McGeoghan
Sent: 22 May 2009 09:26
To: [hidden email]
Subject: Re: extracting parts of string variable

The IP addresses are of varying format:
xx.xxx.x.xx
xx.xx.x.xx
xx.xxx.xxx.xx
xxx.xxx.xx.xx

So really I need to find some way of finding the first full stop and
then
anything to the left of that, store in new variable.
Then find the 2nd full stop and anything to the left of that, store in
new
variable.

=====================
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: extracting parts of string variable

Paul Mcgeoghan
In reply to this post by Paul Mcgeoghan
John,

Stub1 combines ip2, ip3 and ip4
I need it to combine ip1, ip2

Stub2 combines ip3 and ip4.
I need it to combine ip1, ip2 and ip3

So I have IP address:
123.124.125.1
I require Stub1 to give me 123.124 (currently it gives me 124.125.1)
I require Stub2 to be 123.124.125 (currently gives 125.1)

Thanks
Paul

=====================
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: extracting parts of string variable

Paul Mcgeoghan
In reply to this post by Paul Mcgeoghan
John,

I concatenated the 2 IP addresses I needed based on your syntax via:

STRING  IP_Range1 (A8).
COMPUTE IP_Range1=CONCAT(RTRIM(ip1),'.',RTRIM(ip2)).
EXECUTE.

STRING  IP_Range2 (A12).
COMPUTE IP_Range2=CONCAT(RTRIM(ip1),'.',RTRIM(ip2),'.',RTRIM(ip3)).
EXECUTE.

Thanks,
Paul

=====================
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: extracting parts of string variable

djhurio
Just another shorter solution.


data list list /IP (a15).
begin data
12.123.124.12
123.2.33.222
333.222.111.222
2.3.2.1
22.2.222.2
end data.

string IP_Range1 (a7) IP_Range2 (a11).

comp IP_Range2=sub(IP,1,rindex(IP,".")-1).
comp IP_Range1=sub(IP_Range2,1,rindex(IP_Range2,".")-1).

exe.



Martins


Paul Mcgeoghan wrote
John,

I concatenated the 2 IP addresses I needed based on your syntax via:

STRING  IP_Range1 (A8).
COMPUTE IP_Range1=CONCAT(RTRIM(ip1),'.',RTRIM(ip2)).
EXECUTE.

STRING  IP_Range2 (A12).
COMPUTE IP_Range2=CONCAT(RTRIM(ip1),'.',RTRIM(ip2),'.',RTRIM(ip3)).
EXECUTE.

Thanks,
Paul

=====================
To manage your subscription to SPSSX-L, send a message to
LISTSERV@LISTSERV.UGA.EDU (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: extracting parts of string variable

Carlos Renato (www.estatistico.org)
Compare the LENGTH of string that contains the IP and
use a conditional statement to COMPUTE a new variable.
Other mean is verify the appearance of the point (".") in
the positions and adapt a conditional compute to this.

Carlos Renato
Reply | Threaded
Open this post in threaded view
|

Re: extracting parts of string variable

John McConnell-2
In reply to this post by Paul Mcgeoghan
Paul

I'm with you now

Glad it worked (partially)

Cheers

John

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Paul McGeoghan
Sent: 22 May 2009 11:41
To: [hidden email]
Subject: Re: extracting parts of string variable

John,

I concatenated the 2 IP addresses I needed based on your syntax via:

STRING  IP_Range1 (A8).
COMPUTE IP_Range1=CONCAT(RTRIM(ip1),'.',RTRIM(ip2)).
EXECUTE.

STRING  IP_Range2 (A12).
COMPUTE IP_Range2=CONCAT(RTRIM(ip1),'.',RTRIM(ip2),'.',RTRIM(ip3)).
EXECUTE.

Thanks,
Paul

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