DATA Step, Macro, Functions and more

Trim Values that contain space

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 121
Accepted Solution

Trim Values that contain space

[ Edited ]

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!

 

 


Accepted Solutions
Solution
‎01-12-2016 03:19 PM
Super User
Posts: 10,550

Re: Trim Values that contain space

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;

View solution in original post


All Replies
Contributor
Posts: 55

Re: Trim Values that contain space

[ Edited ]

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)

 

 

 

Super User
Posts: 5,099

Re: Trim Values that contain space

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.

Frequent Contributor
Posts: 121

Re: Trim Values that contain space

If I specified the set length is $5, then am I able to delete any characters following the 5th spot?
Super User
Posts: 5,099

Re: Trim Values that contain space

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.

Solution
‎01-12-2016 03:19 PM
Super User
Posts: 10,550

Re: Trim Values that contain space

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;

Frequent Contributor
Posts: 121

Re: Trim Values that contain space

Thank you @ballardw !!
Frequent Contributor
Posts: 121

Re: Trim Values that contain space

@ballardw ...how would I write this code if I want to do the same for just one variable (principaldx)? I'm assuming I would not use an array...
Super User
Posts: 17,963

Re: Trim Values that contain space

Replace the array references with references to your actual variable.

And remove the do loop portion. 

Super User
Posts: 10,550

Re: Trim Values that contain space

To replace your current variable:
principaldx = scan(principaldx,1);
Scan also allows setting a list of characters to separate your words if needed.
Super User
Posts: 17,963

Re: Trim Values that contain space

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.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 10 replies
  • 417 views
  • 5 likes
  • 5 in conversation