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

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!

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