BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jenim514
Pyrite | Level 9

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!

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

10 REPLIES 10
JoshB
Quartz | Level 8

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)

 

 

 

Astounding
PROC Star

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.

jenim514
Pyrite | Level 9
If I specified the set length is $5, then am I able to delete any characters following the 5th spot?
Astounding
PROC Star

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.

ballardw
Super User

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;

jenim514
Pyrite | Level 9
Thank you @ballardw !!
jenim514
Pyrite | Level 9
@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...
Reeza
Super User

Replace the array references with references to your actual variable.

And remove the do loop portion. 

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

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 10 replies
  • 2055 views
  • 6 likes
  • 5 in conversation