01-12-2016 02:25 PM - edited 01-12-2016 02:36 PM
Hi! I am trying to figure out how to trim the values of variables that contain a space. I do not want to compress or concatenate the value but rather completely delete any characters that follow a blank space in the value field. For example, I have several Procedure codes: Proc1-Proc4.
PROC1 PROC2 PROC3 PROC4
7501 D01 0193 D01 90411 D02 80411 D02
I want to completly delete any characters that follow the space. How might I accomplis this?
01-12-2016 02:37 PM - edited 01-12-2016 02:39 PM
Assuming the first space you come across is always your delimiter, you can use the index function to find out where in the string that space is (index function), and only keep the text to the left of it using substring function. e.g
var2 = substr(var,1,index(var," ")-1)
01-12-2016 02:39 PM
This is not possible in SAS. Character variables always have a set length, which cannot vary from one observation to the next. For example:
if sex='F' then gender='Female';
GENDER will always contain six characters. Its length is set and stays set.
For reporting purposes, or for writing out the data, you will have choices about whether to write out trailing blanks. But character variables never change their length from one observation to the next.
01-12-2016 03:19 PM
No. Once you set the length of $5, it stays $5. For example:
length varname $ 5;
varname = 'ABC';
SAS is storing 5 characters, "ABC" plus two blanks. The length of the variable is set.
If you are looking to save space, you can compress the data set.
If you are looking to combine character strings, you can use the nonblanks only. For example:
length newvar $ 5;
newvar = trim(varname) || '01';
This gives you "ABC01", and doesn't use the trailing blanks at the end of VARNAME. But the length of VARNAME is constant across all observations.
varname = trim(varname);
The TRIM function really works. It really returns just three characters: "ABC". However, SAS then stores them in VARNAME which has a set length of $5. By doing that, SAS adds those two blanks back onto the end of the value.
01-12-2016 03:36 PM
I think a small issue here is terminology, you're not looking to trim, but really to create a substring or extract a portion from text.
TRIM() in SAS is used to remove leading/trailing blanks when required.
Need further help from the community? Please ask a new question.