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!!
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;
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 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;
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!
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.