View previous topic :: View next topic
|
Author |
Message |
venktv
New User
Joined: 29 Mar 2006 Posts: 59 Location: Montreal
|
|
|
|
Hi Friends,
The requirement is to remove the trailing ‘{‘ from a variable using SAS code.
For eg.:
If Var1 is ‘ABC}}}}}}}}}’ then only ‘ABC’ should be retained
If Var1 is ‘ABC}}}}}}}XYX’ then ‘ABC}}}}}}}XYX’ should be the output
If Var1 is ‘ABC}}}}}}}XYX}}}}}}}}}’ then ‘ABC}}}}}}}XYX’ should be the output
If Var1 is ‘}}}}}}}}}}’ then the output should be null
Can you pls help me to write a logic in SAS language
Thanks |
|
Back to top |
|
|
Kevin Wailes
New User
Joined: 29 Aug 2008 Posts: 10 Location: UK
|
|
|
|
Try something like :-
x = INDEX(var1,'{');
x = x - 1;
var2 = SUBSTR(var1,1,x); |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10886 Location: italy
|
|
|
|
fuzzy logic....
the OP asks for a SAS solution to remove a trailing "{"
... in a cobol forum
... the sample strings contain only "}"
... gets a rexx solution...
|
|
Back to top |
|
|
expat
Global Moderator
Joined: 14 Mar 2007 Posts: 8796 Location: Welsh Wales
|
|
|
|
Quote: |
fuzzy logic....
the OP asks for a SAS solution to remove a trailing "{"
... in a cobol forum
... the sample strings contain only "}"
... gets a rexx solution...
|
Aaaaaaaah Enrico, it must be Monday.
Problem is accepting the internal possibilities, but try this UNTESTED code
Code: |
DATA OUT01;
INFILE WORKIN;
INPUT VAR01 $CHAR44.;
VAR01 = TRIM(VAR01);
A = 1;
DO WHILE (A = 1);
LASTCHAR = SUBSTR(VAR01,LENGTH(VAR01),1);
IF LASTCHAR NE '}'
THEN A = 0;
ELSE DO;
IF VAR01 = '}' THEN DO;
A = 0;
VAR01 = ' ';
END;
ELSE
VAR01 = SUBSTR(VAR01,1,LENGTH(VAR01)-1);
END;
END;
|
|
|
Back to top |
|
|
venktv
New User
Joined: 29 Mar 2006 Posts: 59 Location: Montreal
|
|
|
|
Hi enrico,
Where is the SAS forum?... there is separate forum for SAS.. so posted here.... anyway ... thanks for the suggestion... but i want it in SAS not in REXX.. |
|
Back to top |
|
|
venktv
New User
Joined: 29 Mar 2006 Posts: 59 Location: Montreal
|
|
|
|
Hi Expat,
Thanks very much for the logic.. its working
Thank you.... |
|
Back to top |
|
|
Kevin Wailes
New User
Joined: 29 Aug 2008 Posts: 10 Location: UK
|
|
|
|
Actually not REXX but using SAS functions |
|
Back to top |
|
|
Kevin Wailes
New User
Joined: 29 Aug 2008 Posts: 10 Location: UK
|
|
|
|
And to prove the point :-
1 DATA;
2 VAR1 = 'ABCDEF}}}}}';
3 X = INDEX(VAR1,'}');
4 X = X - 1;
5 VAR2 = SUBSTR(VAR1,1,X);
6 RUN;
NOTE: THE DATA SET WORK.DATA1 HAS 1 OBSERVATIONS AND 3 VARIABLES.
NOTE: COMPRESSING DATA SET WORK.DATA1 INCREASED SIZE BY 100.00 PERCENT.
COMPRESSED IS 2 PAGES; UN-COMPRESSED WOULD REQUIRE 1 PAGES.
2 THE SAS SYSTEM
NOTE: THE DATA STATEMENT USED 0.00 CPU SECONDS AND 10463K.
6 !
7 PROC PRINT;
8 RUN;
NOTE: THERE WERE 1 OBSERVATIONS READ FROM THE DATA SET WORK.DATA1.
NOTE: THE PROCEDURE PRINT PRINTED PAGE 1.
NOTE: THE PROCEDURE PRINT USED 0.01 CPU SECONDS AND 11311K.
8 !
THE SAS SYSTEM
OBS VAR1 X VAR2
1 ABCDEF}}}}} 6 ABCDEF |
|
Back to top |
|
|
expat
Global Moderator
Joined: 14 Mar 2007 Posts: 8796 Location: Welsh Wales
|
|
|
|
Quote: |
If Var1 is ‘ABC}}}}}}}XYX}}}}}}}}}’ then ‘ABC}}}}}}}XYX’ should be the output |
Unfortunately the INDEX function will return the first occurance and from the OP's original post, quote above, the method shown in your last post would not give the correct result.
Result would be ABC |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8700 Location: Dubuque, Iowa, USA
|
|
|
|
Code: |
var1rev = reverse(var1) ;
do while (substr(var1rev,1,1) = '}');
var1rev = substr(var1rev, 2) ;
end;
var1 = reverse(var1rev); |
|
|
Back to top |
|
|
Kevin Wailes
New User
Joined: 29 Aug 2008 Posts: 10 Location: UK
|
|
|
|
Didn't spot that case in the original requirement. Should have.
Code: |
DATA;
VAR1 = 'ABCDEF}}}}}}}}}GHI';
L = LENGTH(VAR1);
DO I=LENGTH(VAR1) TO 1 BY -1 WHILE (SUBSTR(VAR1,I,1) = '}');
L = L - 1;
END;
VAR2 = SUBSTR(VAR1,1,L);
RUN;
PROC PRINT;
RUN; |
|
|
Back to top |
|
|
expat
Global Moderator
Joined: 14 Mar 2007 Posts: 8796 Location: Welsh Wales
|
|
Back to top |
|
|
expat
Global Moderator
Joined: 14 Mar 2007 Posts: 8796 Location: Welsh Wales
|
|
|
|
Quote: |
If Var1 is ‘}}}}}}}}}}’ then the output should be null |
You're FIRED twice |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8700 Location: Dubuque, Iowa, USA
|
|
|
|
Quote: |
If Var1 is ‘}}}}}}}}}}’ then the output should be null |
Since SAS string variables don't actually have NULL fields because they're fixed length, the output string is set to spaces in my code (which was tested, btw). |
|
Back to top |
|
|
|