DATA Step, Macro, Functions and more

Removing Trailing Blanks

Accepted Solution Solved
Reply
Super Contributor
Posts: 1,041
Accepted Solution

Removing Trailing Blanks

Hi Team,

At the outset I would like to wish you a Merry Christmas.

Secondly,

I have a variable with codes and description comined under a single variable like shown.

Codes

111 Diabetes

222 Cancer

333 Diabetes low

444 Diabetes High

I had to seperate the codes from the description. My code is as follows;

data codes;

set Bench;

code2=strip(subsr(code,1,3));

run;

Even then I get the trailing spaces in code2 variable and its length remains the same as the original variable????

Could you help me remove the spaces?i could not acheive it with the trim either!!

Regards


Accepted Solutions
Solution
‎12-26-2012 10:49 AM
PROC Star
Posts: 7,468

Re: Removing Trailing Blanks

Posted in reply to robertrao

Is there an issue with blanks or just with the new variables length?  If it is the latter, is it corrected if you add a length statement?  e.g.:

data codes;

  set Bench;

  length code2 $3;

  code2=strip(substr(code,1,3));

run;

View solution in original post


All Replies
Solution
‎12-26-2012 10:49 AM
PROC Star
Posts: 7,468

Re: Removing Trailing Blanks

Posted in reply to robertrao

Is there an issue with blanks or just with the new variables length?  If it is the latter, is it corrected if you add a length statement?  e.g.:

data codes;

  set Bench;

  length code2 $3;

  code2=strip(substr(code,1,3));

run;

Super Contributor
Posts: 1,041

Re: Removing Trailing Blanks

Thanks a lot.

Respected Advisor
Posts: 3,156

Re: Removing Trailing Blanks

Posted in reply to robertrao

Karun,

Scan(), substr(), trim(), strip() and many other character functions do NOT assign length to your new variable, they only inherit the length of the variables they are working on. In your case, "code2" will have the same length as "code" if you don't explicitly assign a length to it.

Haikuo

update for your code:

data have;

input code $&100.;

cards;

111 Diabetes

222 Cancer

333 Diabetes low

444 Diabetes High

;

data want;

  set have;

  length code2 $3;

code2 = left(code);

run;

🔒 This topic is solved and locked.

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

Discussion stats
  • 3 replies
  • 244 views
  • 3 likes
  • 3 in conversation