f



Remove first and last 3 characters of a string

Hi,


What are the possible ways of removing first character and last 3
characters from a string.
eg. variable values: A876479003, A675846003, A6842003.
     I am expecting the following values: 876479, 675846, 6842

Thanks,
Sri
0
Sri
5/14/2010 9:30:25 PM
comp.soft-sys.sas 142827 articles. 4 followers. Post Follow

1 Replies
9938 Views

Similar Articles

[PageSpeed] 36

You can use LENGTH() function to determine how long the string is then use 
SUBSTR() function to capture the range of the characters you want to keep.

"Sri" <sriseepana@gmail.com> wrote in message 
news:f4fc456a-d283-4240-ab2b-c5117ace4b50@s4g2000prh.googlegroups.com...
> Hi,
>
>
> What are the possible ways of removing first character and last 3
> characters from a string.
> eg. variable values: A876479003, A675846003, A6842003.
>     I am expecting the following values: 876479, 675846, 6842
>
> Thanks,
> Sri 

2
Kenneth
5/16/2010 7:10:02 AM
Reply:

Similar Artilces:

removing first and last character of a string
I can't believe I can't figure out how to do this. It seems so simple to explain, so please bear with me. I want to remove the first and last character from all the cells (all variables, all observations) in my dataset. So in the end I want all the variables to be 2 characters shorter, since they will be missing both the first and the last character from the original variable. (I want to do this so that I can ulimately compare DatasetA to DatasetB, which should be the same except that all the cells in DatasetA are currently enclosed in quotes whereas in DatasetB they are not. Of c...

remove last 3 characters from a string
Hi, What are the possible ways of removing first character and last 3 characters from a string. eg. variable values: A876479003, A675846003, A6842003 Thanks, Sri ...

Re: How to convert a string of character 40 to a string of characters #3
Mark's one-liner can be simplified to avoid use of SUBSTR: nibbles = binary (rank (translate (hexarr, '0a0b0c0d0e0f0a0b0c0d0e0f00010203040506070809'x, 'abcdefABCDEF0123456789')), 4); "robin" <robin_v@bigpond.mapson.com> wrote in message news:fR0od.44049$K7.5489@news-server.bigpond.net.au... > Mark's one-liner can be simplified to avoid use of SUBSTR: > > nibbles = binary (rank (translate (hexarr, > '0a0b0c0d0e0f0a0b0c0d0e0f00010203040506070809'x, > ...

removing only the last few specific characters in a string
I have a dataset with a column as follows : I need to remove only the last = '|' characters if available in a string. I tried tranwrd to replace '|' to = ' '( space) and then trim to remove trailing blanks and then again transwrd= to replace ' ' to '|' . But unable to solve.=20 Have : Need : column1 Column1=20 R1|R2|||| R1|R2 R1|R2 R1|R2 R1|R2|R3|| R1|R2|R3 R1R2|| R1R2 On Friday, September 6, 2013 6:20:02 AM UTC-7, m.chila...@gmail.com wr...

removing the first 15 characters from a string
hi, I am trying to remove the first 15 characters from a filename. I used to have the following: for i in `ls *.DAT` do mv $i `echo $i | awk -F_ '{print $2}'` done which worked fine because my date/timestamp was followed by the ONLY "_" in the filename. now, I need to remove the date / timestamp: "20060131035927_" from the filenames. They are always the first 15 characters of the filename. Let me know if there is an easy way to do it ( I know there is, but I'm a bit stumped here. ) Thanks guys. :L I should have said that 20060131035927_ is just an e...

How to remove the first 5 characters from a string?
For example, the string is: myStr1122334455. Remove "myStr" and the string becomes: 1122334455. Thanks. On 2010-05-26, Jack wrote: > For example, the string is: myStr1122334455. Remove "myStr" and the > string becomes: 1122334455. string=myStr1122334455 In any standard Unix shell: newstring=${string#?????} In bash or ksh93: newstring=${string:5} -- Chris F.A. Johnson, author <http://shell.cfajohnson.com/> =================================================================== Shell Scripting Recipes: A Probl...

Re: How to convert a string of character 40 to a string of character 20 in PL/I #3
"David Frank" <dave_frank@hotmail.com> writes: > > "robin" <robin_v@bigpond.mapson.com> wrote in message > news:Qifmd.37658$K7.13124@news-server.bigpond.net.au... > > From: "David Frank" <dave_frank@hotmail.com>, Road Runner High Speed > > Online http://www.rr.com > > Date: Mon, 15 Nov 2004 11:56:34 GMT > > > > | "kundu" <subhadarsan_kundu@yahoo.com> wrote in message > > news:90b6fc24bea5b09a965fb1f1da016820@localhost.talkaboutprogramming.com... > > | > Hi all, > > | ...

Remove the first 3 characters after reading each line
Hi , I am new to unix . I have a file to read and after reading each line I want to strip off the first 3 characters and store in another variable. Cut command cuts the first 3 characters but I do not how to get the rest of letters. For ex: I have a line XXX_JOBNAME Here is the code cat jobs.txt |cut -c1-4 This prints XXXX_. I want JOBNAME to be printed .But length of JOBNAME differs in each line. #Logic ext=".ksh" for line in `cat ~/jobs.txt` do echo $line #remove the XXX_ #put the cut logic here a...

Remove last two characters of a string variable
Hi, I need to remove the last two characters of a string variable, all of which= differ in their last two characters. For instance, from AL0011200 -> AL001= 12. Whereas I know how to delete it if all the observations were of the sam= e length, in this dataset, the number of characters varies substantially. = =20 Many thanks in advance, Scott On 28/06/2013 7:43 PM, walfields@gmail.com wrote: > Hi, > > I need to remove the last two characters of a string variable, all of which differ in their last two characters. For instance, from AL0011200 -> AL00112. Whereas I know how to delete it if all the observations were of the same length, in this dataset, the number of characters varies substantially. > > Many thanks in advance, > Scott > From the FM: CHAR.LENGTH. CHAR.LENGTH(strexpr). Numeric. Returns the length of strexpr in characters, with any trailing blanks removed. CHAR.SUBSTR. CHAR.SUBSTR(strexpr,pos[,length]). String. Returns the substring beginning at character position pos of strexpr. The optional third argument represents the number of characters in the substring. If the optional argument length is omitted, returns the substring beginning at character position pos of strexpr and running to the end of strexpr. For example CHAR.SUBSTR(�abcd�, 2) returns �bcd� and CHAR.SUBSTR(�abcd�, 2, 2) returns �bc�. (Note: Use the SUBSTR function instead of CHAR.SUBSTR if you want to use the function on the left s...

Re: Removing First 5 Characters #3
Mark's solution is the one that I would also use if the strings were always as the ones that you show. If you have one like ab 223 test12 and you allways have letters space number space letters then you could use var = scan ( var , 3 , ' ' ); Nat Wooding Environmental Specialist III Dominion, Environmental Biology 4111 Castlewood Rd Richmond, VA 23234 Phone:804-271-5313, Fax: 804-271-2977 J M <jasonm@UCLA.EDU> Sent by: "SAS(r) To Discussion" SAS-L...

Re: Is there a way to find out last(first.) and last(last.) in #3
Is your program suppose to work? 1190 data Work.New2; 1191 1192 if 0 then set Work.New1 nobs = Nrows; 1193 drop LastId; 1194 retain LastId; 1195 if 0 then set Work.New1 firstobs = Nrows; - 22 76 ERROR 22-322: Syntax error, expecting one of the following: END, KEY, KEYS, NOBS, POINT. ERROR 76-322: Syntax error, statement will be ignored. 1196 LastId = Id; 1197 1198 do until(EndoFile); 1199 set Work.New1 1200 (where = (Id eq LastId)) 1201 end = EndoFile; ERR...

Re: SAS Quiz: Nasty Character Strings #3
Lou: Thanks. I think this method will work. Sorry for any confusion in my post. I am in the process of organizing all of the .sas programs and data sets that were used by a retired colleague. In a sense, I am cataloging the .sas files, all of his comments, and all of the data sets that he used. Your solution works like a charm. Thanks, Lewis ...

Remove the first 3 characters after reading each line #2
Hi , I am new to unix . I have a file to read and after reading each line I want to strip off the first 3 characters and store in another variable. Cut command cuts the first 3 characters but I do not how to get the rest of letters. For ex: I have a line XXX_JOBNAME Here is the code cat jobs.txt |cut -c1-4 This prints XXXX_. I want JOBNAME to be printed .But length of JOBNAME differs in each line. #Logic ext=".ksh" for line in `cat ~/jobs.txt` do echo $line #remove the XXX_ #put the cut logic here a...

How to replace the last (and only last) character in a string?
Let's suppose s='12345 4343 454' How can I replace the last '4' character? I tried string.replace(s,s[len(s)-1],'r') where 'r' should replace the last '4'. But it doesn't work. Can anyone explain why? Thanks L. On May 3, 9:27 am, Johny <pyt...@hope.cz> wrote: > Let's suppose > s='12345 4343 454' > How can I replace the last '4' character? > I tried > string.replace(s,s[len(s)-1],'r') > where 'r' should replace the last '4'. > But it doesn't work. > Can anyone explain why? > > Thanks > L. I think the reason it's not working is because you're doing it kind of backwards. For one thing, the "string" module is deprecated. I would do it like this: s = s.replace(s[len(s)-1], 'r') Although that is kind of hard to read. But it works. Mike On May 3, 4:37 pm, kyoso...@gmail.com wrote: > On May 3, 9:27 am, Johny <pyt...@hope.cz> wrote: > > > Let's suppose > > s='12345 4343 454' > > How can I replace the last '4' character? > > I tried > > string.replace(s,s[len(s)-1],'r') > > where 'r' should replace the last '4'. > > But it doesn't work. > > Can anyone explain why? > > > Thanks > > L. > > I think the reason it's not working is because you're doing it kind of > backwards. For one ...

Re: SAS term: character string, text expression #3
Don't Lisp, Forth, and related programming languages allow all sorts of liberties in definitions of functions and procedures? As I recall, a Lisp EVAL() function attempts to execute any expression. As for the lack of a distinction between variables and program, OOP deliberately blurs that faint line. Perhaps SAS in this case, as with logical expressions, blazed the trail into 4th generation programming languages. I don't recall a specific point at which the SAS language evolved into a compile-and-go language with step boundaries, or who led that effort. By the time programmers began r...

[rcr] String#first / String#last
problem: I often use a range to extract the beginning/ending letters.. like this "string"[-4, 4] #-> "ring" It feels somewhat redundant that I have to type the length 2 times. A very simple operation that easily can confuse people if they are browsing through others code.. this is a place where one easily can introduce off-by-one errors. motivation: When looking at the Array class I see that it has #first and #last methods.. just for this purpose, to extract the sub-array either from the beginning or the ending. Why not also use #first and #last on String ? prop...

Fastest way to remove the first x characters from a very long string
I have a string that contains 10 million characters. The string is formatted as: "0000001 : some hexadecimal text ... \n 0000002 : some hexadecimal text ... \n 0000003 : some hexadecimal text ... \n .... 0100000 : some hexadecimal text ... \n 0100001 : some hexadecimal text ... \n" and I need the string to look like: "some hexadecimal text ... \n some hexadecimal text ... \n some hexadecimal text ... \n .... some hexadecimal text ... \n some hexadecimal text ... \n" I can split the string at the ":" then iterate through the list removing t...

Re: Find position of last occurrence of character in a string #3
hi ... if a string can end with '//' , you need LOCB, otherwise LOCA is OK data test; input string $30.; datalines; abc/abc/abc/333 xyz/999 rty//uu abcdefg// qqq ; run; data test; set test; loca = length(string) - length(scan(string,-1,'/')); locb = length(catt(string,'*')) - length(scan(catt(string,'*'),-1,'/')); run; proc print data=test; run; -- Mike Zdeb U@Albany School of Public Health One University Place Rensselaer, New York 12144-3456 P/518-402-6479 F/630-604-1475 > JDB, > > You could always use the REVERSE function on the string ...

remove the last character or the newline character?
Hello all: I have the following snippet: In [1]: fileName = 'Perfect Setup.txt\n' In [2]: fileName = fileName[0:len(fileName)-1)] # remove the '\n' character In [3]: fileName Out[3]: 'Perfect Setup.txt' Question one: Does python provide any function that can remove the last character of a string? I don't know whether or not the method I used is efficient Question two: Does python provide any function that can remove the newline character from a string if it exists? Thank very much! -Daniel Daniel Mark wrote: > Question one: > Does python provide an...

Re: Creating character string with first.variable type approach #3
Using the do until last. technique is a tidy solution... Data work.a; input id1 id2 time key $ @@; cards; 1 10 15556 A 1 10 15556 B 1 20 15556 X 2 20 15678 C ;;;; run; data work.b; do until(last.id2); set work.a; by id1 id2; length newKey $20; newKey = trimn(newKey)||','||key; end; newKey = substr(newKey,2); drop key; run; proc print; run; On 4/28/06, Shukla Kshirsagar <shuklak@hotmail.com> wrote: > Good morning all, > > A simple version of a data set I have looks like this: > > id1 id2 time key &...

Remove all string characters from a string and leave numerics
Hi all, This is a data cleansing issue where I wish to remove all string characters from a string variable called price and convert it into a numeric. The variable comes from a question "How much do you spend a year?" (all strings are uppercase if that helps) I tried COMPUTE newvar=3DREPLACE(price,'A' ,' '). but don't know how to loop or repeat this for all sting characters Also note the last entry is 100-300 so I'd need someway to split them into 2 seperate variables Newvar1 newvar2. Very messy problem I know but any hints or pointers greatly appreciated - hopefully there's an SPSS function i dont know of! DATA LIST LIST /price(A20). BEGIN DATA '=A32000 PER YEAR ' =A31,500.00 'I DONT KNOW' 1200 1200 'DOES NOT KNOW' 10000 'DK' 1516 '2740 APPROX' '=A390 PMONTH' '100-300' END DATA. Regards DaveL On Sep 4, 12:00=A0pm, DaveL <davidll...@mail2lloyd.com> wrote: > Hi all, > > This is a data cleansing issue where I wish to remove all string > characters from a string variable called price and convert it into a > numeric. The variable comes from a question "How much do you spend a > year?" (all strings are uppercase if that helps) > > I tried COMPUTE newvar=3DREPLACE(price,'A' ,' '). =A0but don't know how t= o > loop or repeat this for all sting characters > Also note the las...

removing first occurence of a string in a given string
hi all, is there a way i can identify (to remove) the first occurence of a string in a given string (text variable) i.e. i have values such as 'Block 01' and 'Block 01' --- the 2nd instance has 2 spaces, and i wanna get rid of the first one? thanks On Oct 9, 3:36 pm, "Eversmann Inc." <rifazraz...@gmail.com> wrote: > hi all, > > is there a way i can identify (to remove) the first occurence of a > string in a given string (text variable) > > i.e. > i have values such as 'Block 01' and 'Block 01' --- the 2nd instance >...

Re: removing first occurence of a string in a given string
Looking at SAS text functions might help.(COMPBL??) -----Original Message----- From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of Eversmann Inc. Sent: Tuesday, October 09, 2007 8:07 PM To: SAS-L@LISTSERV.UGA.EDU Subject: removing first occurence of a string in a given string hi all, is there a way i can identify (to remove) the first occurence of a string in a given string (text variable) i.e. i have values such as 'Block 01' and 'Block 01' --- the 2nd instance has 2 spaces, and i wanna get rid of the first one? thanks ...

Re: removing first occurence of a string in a given string #2
Wow, some one who took the time to write my full name!!! You're welcome! :-) Kumar -----Original Message----- From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of Master Chief Petty Officer John-117 Sent: Tuesday, October 09, 2007 10:54 AM To: SAS-L@LISTSERV.UGA.EDU Subject: Re: removing first occurence of a string in a given string On Oct 9, 3:36 pm, "Eversmann Inc." <rifazraz...@gmail.com> wrote: > hi all, > > is there a way i can identify (to remove) the first occurence of a > string in a given string (text variable) > > i.e. > ...