How to transfer character variable into specified form?

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

How to transfer character variable into specified form?

I want to transfer ID into the form like "N00012345". 9 digits with capital letter "N" and adding "0" between "N" and raw data.

data have;
	input ID $20.;
cards;
N00012345
23456
123
123456
01234
;

Accepted Solutions
Solution
‎02-24-2016 12:34 PM
Grand Advisor
Posts: 17,420

Re: How to transfer character variable into specified form?

Strip variable of letters, keep only numbers-> COMPRESS() function

Convert to numeric -> INPUT() function

Convert to character with leading zeros -> PUT() function with Z8 format for leading zeros

Concatenate with N-> CATT

 

data have;
	input ID $20.;
	ID_Coded=catt("N", put(input(compress(ID, , 'kd'), 8.), z8.));
cards;
N00012345
23456
123
123456
01234
;
run;

proc print; run;

 

View solution in original post


All Replies
Solution
‎02-24-2016 12:34 PM
Grand Advisor
Posts: 17,420

Re: How to transfer character variable into specified form?

Strip variable of letters, keep only numbers-> COMPRESS() function

Convert to numeric -> INPUT() function

Convert to character with leading zeros -> PUT() function with Z8 format for leading zeros

Concatenate with N-> CATT

 

data have;
	input ID $20.;
	ID_Coded=catt("N", put(input(compress(ID, , 'kd'), 8.), z8.));
cards;
N00012345
23456
123
123456
01234
;
run;

proc print; run;

 

New Contributor
Posts: 2

Re: How to transfer character variable into specified form?

It works!! Thank you so much. I have already spent more than 4 hours for this problem. Thank you again.

☑ This topic is SOLVED.

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

Discussion stats
  • 2 replies
  • 228 views
  • 1 like
  • 2 in conversation