|
I have two string variables (v1 and v2).
v1 v2 10 44 10 4 110 55 220 222 990 9 And I want to combine them to get third variable (v3) like this: v1 v2 v3 10 44 100044 10 4 100004 110 55 1100055 220 222 2200222 990 9 9900009 So, If the width of v2 is less than 4, I will need to add "0" in front. Any suggestions for me? Thanks a lot! Hui ===================== 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 |
|
Hui,
Here is a way to do something like what you want but I don't quite understand the rule for constructing v3. It seems that v3 can be either a6 or a7 and the number of added 0s is not correctly specified to me. String v3(a6). Compute #lv1=length.char(v1). Compute #lv2=length.char(v2). Do if (#lv1+#lv2 eq 3). + compute v3=concat(rtrim(v1,' '),'000',rtrim(v2,' ')). else if (#lv1+#lv2 eq 4). + compute v3=concat(rtrim(v1,' '),'00',rtrim(v2,' ')). else if (#lv1+#lv2 eq 5). + compute v3=concat(rtrim(v1,' '),'0',rtrim(v2,' ')). else if (#lv1+#lv2 eq 6). + compute v3=concat(rtrim(v1,' '),rtrim(v2,' ')). End if. Gene Maguin I have two string variables (v1 and v2). v1 v2 10 44 10 4 110 55 220 222 990 9 And I want to combine them to get third variable (v3) like this: v1 v2 v3 10 44 100044 10 4 100004 110 55 1100055 220 222 2200222 990 9 9900009 So, If the width of v2 is less than 4, I will need to add "0" in front. Any suggestions for me? Thanks a lot! Hui ===================== 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 |
|
In reply to this post by Hui Zhao
At 04:20 PM 3/10/2009, Hui Zhao wrote:
I have two string variables (v1 and v2).|-----------------------------|---------------------------| |Output Created |10-MAR-2009 18:42:16 | |-----------------------------|---------------------------| v1 v2 10 44 10 4 110 55 220 222 990 9 Number of cases read: 5 Number of cases listed: 5 And I want to combine them to get third variable (v3) like this: v1 v2 v3 Yes. LPAD is your tool. Tested code: STRING v3 (A8). COMPUTE v3 = CONCAT(LPAD(LTRIM(RTRIM(v1)),4,' '), LPAD(LTRIM(RTRIM(v2)),4,'0')). LIST. List |-----------------------------|---------------------------| |Output Created |10-MAR-2009 18:42:16 | |-----------------------------|---------------------------| v1 v2 v3 10 44 100044 10 4 100004 110 55 1100055 220 222 2200222 990 9 9900009 Number of cases read: 5 Number of cases listed: 5 ============================= APPENDIX: Test data, and code ============================= DATA LIST LIST/ v1 v2 (2A4). BEGIN DATA 10 44 10 4 110 55 220 222 990 9 END DATA. LIST. STRING v3 (A8). COMPUTE v3 = CONCAT(LPAD(LTRIM(RTRIM(v1)),4,' '), LPAD(LTRIM(RTRIM(v2)),4,'0')). LIST. * Code to test the string functions: . STRING v1P1 v1P2 v2P1 v2P2 (A4). COMPUTE v1P1 = LPAD(v1, 4,' '). COMPUTE v1P2 = LPAD(LTRIM(RTRIM(v1)),4,' '). COMPUTE v2P1 = LPAD(v2, 4,'0'). COMPUTE v2P2 = LPAD(LTRIM(RTRIM(v2)),4,'0'). LIST. ===================== 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 |
| Free forum by Nabble | Edit this page |
