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

I am trying to use the strip function to reduce the length of a character variable from 255 characters to 3 characters (the longest value has 3 characters).

 

data lengths;
set courtdata;

new =  strip (ADJCLASS);
run;

 

I have read the thread here: https://communities.sas.com/t5/SAS-Programming/SAS-strip-function-doesn-t-work/td-p/434757

 

But it is unclear from the previous thread how the problem is actually fixed. I am ultimately trying to convert this variable from character to numeric, but I think I will need to strip it first. 

 

Thanks!!

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Are you asking for

 

data sample;
length var_char $255;/*Your length set at compile time-also when reading another dataset*/
var_char='     999          ';
var_num=input(strip(var_char),3.);
put var_char= var_num=;
run;

View solution in original post

4 REPLIES 4
novinosrin
Tourmaline | Level 20

Are you asking for

 

data sample;
length var_char $255;/*Your length set at compile time-also when reading another dataset*/
var_char='     999          ';
var_num=input(strip(var_char),3.);
put var_char= var_num=;
run;
jlutz
Calcite | Level 5
Thank you!
Reeza
Super User
Unless you change the variable length, SAS will add spaces by default to take up the extra room. You need to change the length of the variable for this to be an effective solution.
Tom
Super User Tom
Super User

@jlutz wrote:

I am trying to use the strip function to reduce the length of a character variable from 255 characters to 3 characters (the longest value has 3 characters).

 

data lengths;
set courtdata;

new =  strip (ADJCLASS);
run;

 

I have read the thread here: https://communities.sas.com/t5/SAS-Programming/SAS-strip-function-doesn-t-work/td-p/434757

 

But it is unclear from the previous thread how the problem is actually fixed. I am ultimately trying to convert this variable from character to numeric, but I think I will need to strip it first. 

 

Thanks!!


There is no need to do anything to the original value, unless it has LEADING spaces. You want to use the LEFT() function in case the value is not in the first 32 bytes of the string. There is no need to use the STRIP() function since trailing spaces don't matter, but you could use it in place of the LEFT() function.

The maximum number of characters the normal informat for converting text strings into numbers can handle is 32.  So if your actual data has a max of only 3 characters, even though the variable has room for more, then you should not have many issues (although the number of decimal digits a number can actually store is more like 15 digits).

data lengths;
  set courtdata;
  new =  input(left(ADJCLASS),32.);
run;

 

SAS Innovate 2025: Register Now

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!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 1733 views
  • 3 likes
  • 4 in conversation