String data manipulations

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

String data manipulations

Rajeshms
Hi All,

I have a string variable as below

codes
12.32
123.356
d12.36
e12.36
e12.00
er12.3
ra21.365

Now I need to get only the numbers before decimal point as below

codes
12
123
d12
e12
e12
er12
ra21

Can anyone help on this. Thanks all.
--
Regards,

Rajesh M S




Reply | Threaded
Open this post in threaded view
|

Re: String data manipulations

PRogman
Please read about string functions (HELP| Command Syntax Reference | Universals | Transforming Expressions | String functions).
/PR

DATA LIST FREE / codes(a8).
BEGIN DATA
'12.32'
'123.356'
'd12.36'
'e12.36'
'e12.00'
'er12.3'
'ra21.365'
END DATA.

STRING result (A8) .
COMPUTE result=char.substr(codes, 1, CHAR.INDEX(codes,'.')-1) .
EXECUTE.
LIST codes result .

codes    result
 
12.32    12
123.356  123
d12.36   d12
e12.36   e12
e12.00   e12
er12.3   er12
ra21.365 ra21
 
 
Number of cases read:  7    Number of cases listed:  7
Reply | Threaded
Open this post in threaded view
|

Re: String data manipulations

David Marso
Administrator
PR:  Your answer assumes there will ALWAYS be a .
If there is not a . the result will be blank and yield a warning ;-(
codes    result   result2
 
12.32    12       12
123.356  123      123
d12.36   d12      d12
e12.36   e12      e12
e12.00   e12      e12
er12.3   er12     er12
ra21.365 ra21     ra21
 
>Warning # 651
>The third argument to the CHAR.SUBSTR function is invalid.
>Command line: 786  Current case: 8  Current splitfile group: 1
 
1234567           1234567
1.20000  1        1
 
 
Number of cases read:  9    Number of cases listed:  9

PRogman wrote
Please read about string functions (HELP| Command Syntax Reference | Universals | Transforming Expressions | String functions).
/PR

DATA LIST FREE / codes(a8).
BEGIN DATA
'12.32'
'123.356'
'd12.36'
'e12.36'
'e12.00'
'er12.3'
'ra21.365'
END DATA.

STRING result (A8) .
COMPUTE result=char.substr(codes, 1, CHAR.INDEX(codes,'.')-1) .
EXECUTE.
LIST codes result .

codes    result
 
12.32    12
123.356  123
d12.36   d12
e12.36   e12
e12.00   e12
er12.3   er12
ra21.365 ra21
 
 
Number of cases read:  7    Number of cases listed:  7
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?"
Reply | Threaded
Open this post in threaded view
|

Re: String data manipulations

PRogman
How true!  I just quick-fixed some lines I recently used when stripping diagnose coding which always had a period in position 3 or 4. Should have tested the index to be > 1 and only then do the extraction, otherwise keep the original string value.