Extract left part of the string in SAS?

33,402

Solution 1

Had an impression you want to repeat the substring instead of multiply, so I'm adding REPEAT function just for the curiosity.

proc sql;
select 
  INPUT(SUBSTR('11111111', 1, 4), 4.) * 9            /* if source is char */
, INPUT(SUBSTR(PUT(11111111, 16. -L), 1, 4), 4.) * 9  /* if source is number */
, REPEAT(SUBSTR(PUT(11111111, 16. -L), 1, 4), 9)      /* repeat instead of multiply */
FROM SASHELP.CLASS (obs=1)
;
quit;

Solution 2

substr("some text",1,4) will give you "some". This function works the same way in a lot of SQL implementations.

Also, note that this is a string function, but in your example you're applying it to a number. SAS will let you do this, but in general it's wise to control you conversion between strings and numbers with put() and input() functions to keep your log clean and be sure that you're only converting where you actually intend to.

Solution 3

You might be looking for SUBSTRN function..

SUBSTRN(string, position <, length>)

Arguments

string specifies a character or numeric constant, variable, or expression.

If string is numeric, then it is converted to a character value that uses the BEST32. format. Leading and trailing blanks are removed, and no message is sent to the SAS log.

position is an integer that specifies the position of the first character in the substring.

length is an integer that specifies the length of the substring. If you do not specify length, the SUBSTRN function returns the substring that extends from the position that you specify to the end of the string.

Solution 4

As others have pointed out, substr() is the function you are looking for, although I feel that a more useful answer would also 'teach you how to fish'.

A great way to find out about SAS functions is to google sas functions by category which at the time of writing this post will direct you here:

SAS Functions and CALL Routines by Category

It's worth scanning through this list at least once just to get an idea of all of the functions available.

If you're after a specific version, you may want to include the SAS version number in your search. Note that the link above is for 9.2.

If you have scanned through all the functions, and still can't find what you are looking for, then your next option may be to write your own SAS function using proc fcmp. If you ever need assistance with doing this than I suggest posting a new question.

Share:
33,402
staq
Author by

staq

Updated on August 05, 2022

Comments

  • staq
    staq almost 2 years

    Is there a function SAS proc SQL which i can use to extract left part of the string.it is something similar to LEFT function sql server. in SQL I have left(11111111, 4) * 9 = 9999, I would like to something similar in SAS proc SQL. Any help will be appreciated.

  • Joe
    Joe over 9 years
    SUBSTRN would not be idiomatic in most cases; SUBSTRN is basically the same thing as SUBSTR except that it can return a null string, while SUBSTR cannot (in most cases in SAS, null string is impossible, so a few functions were later added TRIMN and SUBSTRN that can). Normally you wouldn't use that version unless you did need the possibility of a null string.
  • staq
    staq over 9 years
    Many thanks Vasja, Really appriciate the second suits best in my case.it did work.