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

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

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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

3 REPLIES 3
art297
Opal | Level 21

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;

robertrao
Quartz | Level 8

Thanks a lot.

Haikuo
Onyx | Level 15

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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 3 replies
  • 1077 views
  • 3 likes
  • 3 in conversation