Listers,
I have an easy one for you today (I hope). The procedure should be simple. However, I was not able to make the syntax work, neither was I able to find a simple solution to the problem when searching online.
In my datas, there are two variables (V1 and V2), i need to concatenate (or some other function) in order to create a third variable (V3). *SPSS 19 *V1 and V2 are numeric variables
Data looks something like this: V1 123456 987654 V2 9957 7991 V3 (that I wish to create):
1234569957 9876547991 All responses are highly appreciated. Regards, Tore |
Tore, Read up on the concat function in the syntax reference. Your variables are numeric; so you will need to convert them to string before concantenating. So read up on the number function. Gene Maguin From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Tore Viland Listers, I have an easy one for you today (I hope). The procedure should be simple. However, I was not able to make the syntax work, neither was I able to find a simple solution to the problem when searching online. In my datas, there are two variables (V1 and V2), i need to concatenate (or some other function) in order to create a third variable (V3). *SPSS 19 *V1 and V2 are numeric variables Data looks something like this: V1 123456 987654 V2 9957 7991 V3 (that I wish to create): 1234569957 9876547991 All responses are highly appreciated. Regards, Tore |
In reply to this post by Tore Viland
Well, you could convert them to strings,
concatenate them, and then convert them back to numbers, but if the first
value is always six digits and second value is always four digits, the
easiers solution is multiplication of the first value by a power of 10,
followed by addition of the second value.
Rick Oliver Senior Information Developer IBM Business Analytics (SPSS) E-mail: [hidden email] Phone: 312.893.4922 | T/L: 206-4922 From: Tore Viland <[hidden email]> To: [hidden email] Date: 01/26/2012 10:27 AM Subject: CONCAT variables Sent by: "SPSSX(r) Discussion" <[hidden email]> Listers, I have an easy one for you today (I hope). The procedure should be simple. However, I was not able to make the syntax work, neither was I able to find a simple solution to the problem when searching online. In my datas, there are two variables (V1 and V2), i need to concatenate (or some other function) in order to create a third variable (V3). *SPSS 19 *V1 and V2 are numeric variables Data looks something like this: V1 123456 987654 V2 9957 7991 V3 (that I wish to create): 1234569957 9876547991 All responses are highly appreciated. Regards, Tore |
In reply to this post by Tore Viland
data list list /v1 v2.
begin data 123456 789 end data. compute v3=v1*1000+v2. formats v3 (f9). list. Rick Oliver Senior Information Developer IBM Business Analytics (SPSS) E-mail: [hidden email] From: Rick Oliver/Chicago/IBM To: Tore Viland <[hidden email]> Cc: [hidden email] Date: 01/26/2012 10:34 AM Subject: Re: CONCAT variables Well, you could convert them to strings, concatenate them, and then convert them back to numbers, but if the first value is always six digits and second value is always four digits, the easiers solution is multiplication of the first value by a power of 10, followed by addition of the second value. Rick Oliver Senior Information Developer IBM Business Analytics (SPSS) E-mail: [hidden email] From: Tore Viland <[hidden email]> To: [hidden email] Date: 01/26/2012 10:27 AM Subject: CONCAT variables Sent by: "SPSSX(r) Discussion" <[hidden email]> Listers, I have an easy one for you today (I hope). The procedure should be simple. However, I was not able to make the syntax work, neither was I able to find a simple solution to the problem when searching online. In my datas, there are two variables (V1 and V2), i need to concatenate (or some other function) in order to create a third variable (V3). *SPSS 19 *V1 and V2 are numeric variables Data looks something like this: V1 123456 987654 V2 9957 7991 V3 (that I wish to create): 1234569957 9876547991 All responses are highly appreciated. Regards, Tore |
Administrator
|
In reply to this post by Tore Viland
As Rick Oliver said, there are various ways to do it, and which one you choose depends on things you've not told us -- e.g., do the V1 and V2 values always have the same number of digits you show in your examples? And do you want V3 to be numeric or string?
Assuming the number of digits can be variable within V1 and V2 and that you want V3 to be numeric, you could do this: * Read in some sample data. data list list / v1 v2 (2f8.0). begin data 123456 9957 987654 7991 123 45 1 2345678 end data. string # (a16). compute # = concat(string(v1,f8),string(v2,f8)). compute v3 = number(replace(#," ",""),f16). formats v3 (f16.0). list. OUTPUT: v1 v2 v3 123456 9957 1234569957 987654 7991 9876547991 123 45 12345 1 2345678 12345678 Number of cases read: 4 Number of cases listed: 4 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/). |
Tore-- try to the line below
comp v3=v1*10**(trunc(lg10(v2))+1) +v2.
vlad On Thu, Jan 26, 2012 at 9:07 AM, Bruce Weaver <[hidden email]> wrote: As Rick Oliver said, there are various ways to do it, and which one you |
Administrator
|
That's slick. Nice one, Vlad.
--
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/). |
Administrator
|
In reply to this post by Bruce Weaver
data list list / v1 v2 (2f8.0).
begin data 123456 9957 987654 7991 123 45 1 2345678 end data. COMPUTE v3=NUMBER(CONCAT(RTRIM(STRING(V1,F8)),LTRIM(STRING(V2,F8))),F16). FORMAT v3 (F16.0). LIST.
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 |