Help using Base SAS procedures

Do until

Reply
Regular Contributor
Posts: 229

Do until

HI i want to add '0' before the id variable by using do until can any one help me.


data a;
input id $;
len=length(id);
do until (len=5);
a='0';
id=a||id;
end;
stop;
output;
cards;
1
01
001
run;

output
00001
00001
00001
SAS Employee
Posts: 156

Re: Do until

Must it be by using the do loop ?

Or perhaps you can use the following

data a;
input id 8.;
id2=put(id,z5.);
cards;
1
01
001
;
run;
Grand Advisor
Posts: 10,043

Re: Do until

You need to update the len value within the DO loop.
Also, if you want to see the results of each iteration of the loop you need the OUTPUT within the loop as well.


data a;
input id $;
len=length(id);
a='0';
do until (len=5);
id=a||id;
len=length(id);
output;
end;
cards;
1
01
001
run;

You also should consider what might happen if the length of your input variable is greater than 5.
Ask a Question
Discussion stats
  • 2 replies
  • 90 views
  • 0 likes
  • 3 in conversation