Hi SAS community,
I would like to write a substring that uses the variable, "number", starts at position 1 and has a length that stops at 2 places before the last digit. Please advise.
This is what I have tried:
data want;
set have;
new_number=substring(number,1,(-2));
run;
I have also tried this method where I write out a separate substring for each length of the observations for the "number" variable. (This variable contains lengths ranging from 10-20).
data want1;
set have1;
new_number1=substring(number,1,(10-2));
new_number2=substring(number,1,(11-2));
.
..
..
..
new_number20=substring(number,1,(20-2));
run;
Thanks.
It seems to work for me. See code and output below. Are you sure your original text fields don't have any leading blank spaces?
data one;
input MyNumber $ 1-7;
newnumber = substr(MyNumber,1,length(MyNumber)-2);
datalines;
423902
240986
55093
20994
8203986
;;;;
run;
proc print; run;
Obs MyNumber newnumber
1 423902 4239
2 240986 2409
3 55093 550
4 20994 209
5 8203986 82039
If I understand you correctly, how about:
new_number = substring(number,1,length(number)-2);
HTH,
Karl
This code along with codes I previously used did not seem to work as it appears to take anywhere from 2-3 digits off the entire length.
If the code works, shouldn't the output look something like this?
Number New_Number
423902 4239
240986 2409
55093 550
20994 209
8203986 82039
However instead I obtain an output that looks like the last 3 numbers have been removed.
Number New_Number
423092 423
240986 240
55093 55
20994 20
8203986 8203
Thanks.
It seems to work for me. See code and output below. Are you sure your original text fields don't have any leading blank spaces?
data one;
input MyNumber $ 1-7;
newnumber = substr(MyNumber,1,length(MyNumber)-2);
datalines;
423902
240986
55093
20994
8203986
;;;;
run;
proc print; run;
Obs MyNumber newnumber
1 423902 4239
2 240986 2409
3 55093 550
4 20994 209
5 8203986 82039
You are correct. I amended the statement by adding the strip function.
nmbstrip=strip(number);
new_number=substring(nmbstrip,1,length(nmbstrip)-2);
run;
Thanks.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.