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?
Thanks!
I would suggest using SCAN function;
data want;
set have;
array procs proc1 - proc4;
do i = 1 to dim(procs);
procs[i] = scan(procs[i],1);
end;
drop i;
run;
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)
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';
else gender='Male';
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.
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.
I would suggest using SCAN function;
data want;
set have;
array procs proc1 - proc4;
do i = 1 to dim(procs);
procs[i] = scan(procs[i],1);
end;
drop i;
run;
Replace the array references with references to your actual variable.
And remove the do loop portion.
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.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.