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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
KarlK
Fluorite | Level 6

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

4 REPLIES 4
KarlK
Fluorite | Level 6

If I understand you correctly, how about:

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

HTH,

Karl

sophia_SAS
Obsidian | Level 7

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.

KarlK
Fluorite | Level 6

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

sophia_SAS
Obsidian | Level 7

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.

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!

What is Bayesian Analysis?

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.

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
  • 4 replies
  • 778 views
  • 0 likes
  • 2 in conversation