Help using Base SAS procedures

substring and substracting

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 104
Accepted Solution

substring and substracting

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.


Accepted Solutions
Solution
‎03-15-2012 01:21 PM
Contributor
Posts: 65

substring and substracting

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

View solution in original post


All Replies
Contributor
Posts: 65

substring and substracting

If I understand you correctly, how about:

new_number = substring(number,1,length(number)-2);

HTH,

Karl

Frequent Contributor
Posts: 104

substring and substracting

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.

Solution
‎03-15-2012 01:21 PM
Contributor
Posts: 65

substring and substracting

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

Frequent Contributor
Posts: 104

substring and substracting

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.

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 209 views
  • 0 likes
  • 2 in conversation