BookmarkSubscribeRSS Feed
nickspencer
Obsidian | Level 7

Hi,

 

I have the below dataset with multiple customer ids in numeric format. I need to convert them to character variables with leading zeros with 11 character length. How can I do that in a single step for multiple variables?

 

 

data have;
input date date9. cust_id_1 cust_id_2 cust_id_3 cust_id_4 cust_id_5 cust_id_6;
datalines;
01SEP2018 1458976 768976 9856786 7634543 494756 6578436
10SEP2018 859034 7689543 214987 7639856 5674849 35467869
11SEP2018 1254679 1615432 165908 6752143 190876 1098723
;
RUN;

 

 

The final dataset should look as below.

 

date                   cust_id_1     cust_id_2       cust_id_3        cust_id_4       cust_id_5       cust_id_6

01SEP2018 00001458976 00000768976 00009856786 00007634543 00000494756 00006578436
10SEP2018 00000859034 00007689543 00000214987 00007639856 00005674849 00035467869
11SEP2018 00001254679 00001615432 00000165908 00006752143 00000190876 00001098723

 

 

Thanks.

3 REPLIES 3
Jagadishkatam
Amethyst | Level 16

This is an untested  code but should work

 

data have;
input date date9. cust_id_1 cust_id_2 cust_id_3 cust_id_4 cust_id_5 cust_id_6;
datalines;
01SEP2018 1458976 768976 9856786 7634543 494756 6578436
10SEP2018 859034 7689543 214987 7639856 5674849 35467869
11SEP2018 1254679 1615432 165908 6752143 190876 1098723
;
RUN;

data want;
set have;
array charn(6) cust_id_1 cust_id_2 cust_id_3 cust_id_4 cust_id_5 cust_id_6;
array charc(6) $12. id1-id6;
do i = 1 to 6;
charc(i)=put(charn(i),z11.);
end;
run;

 

 

Thanks,
Jag
novinosrin
Tourmaline | Level 20
data have;
input date date9. cust_id_1 cust_id_2 cust_id_3 cust_id_4 cust_id_5 cust_id_6;
datalines;
01SEP2018 1458976 768976 9856786 7634543 494756 6578436
10SEP2018 859034 7689543 214987 7639856 5674849 35467869
11SEP2018 1254679 1615432 165908 6752143 190876 1098723
;
RUN;

data _null_;
set have;
array t(*) cust_id_:;
call symputx('n',dim(t));
stop;
run;



%put &n;

data want;
set have;
array t  cust_id_:;
array cust_id_new(&n) $15;
do _i_=1 to dim(t);
cust_id_new(_i_)=put(t(_i_),z11.);
end;
run;
Kurt_Bremser
Super User

Use sashelp.vcolumn to feed a data step which creates the conversion step with call execute:

data _null_;
set sashelp.vcolumn end=eof (where=(libname = "WORK" and memname = "HAVE" and substr(upcase(name),1,4) = "CUST"));
if _n_ = 1 then call execute("data want; set have;");
call execute('_' !! name !! '= put(' !! name !! ',z11.);');
call execute('drop ' !! name !! ';');
call execute('rename _' !! name !! '=' !! name !! ';');
if eof then call execute('run;');
run;

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 2798 views
  • 1 like
  • 4 in conversation