Convert do loop sequence to Char Variable

Accepted Solution Solved
Reply
Contributor
Posts: 42
Accepted Solution

Convert do loop sequence to Char Variable

Hi Guys,

 

I have this data step:

 

And I want to convert last field - sequence of **i** value to char:

data one;
array c {25} $ ('00' '01' '02' '03' '04' '05' '06' '07' '08' '09' '10' '11' '12' '13' '14' '15' '16' '17' '18' '19' '20' '21' '22' '23' '24');
*c{i}=put(c{i},$2.); ** I tried with this and it does not work**
do i='00' to '24';
output;
end;
run;

 

After execution of data step, I got this in the last field:

i

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

 

and I want:

i

00
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

 

I tried to convert that sequence number in char and I was not able to.

The scope of this that I want to use this **char** sequence like a macro variable.

 

Do you have some work around on this??

 

Thanks and regards,

José

 


Accepted Solutions
Solution
‎02-17-2017 11:15 AM
Contributor
Posts: 42

Re: Convert do loop sequence to Char Variable

Thanks a lot ..It worked Smiley Happy

View solution in original post


All Replies
Super User
Posts: 10,454

Re: Convert do loop sequence to Char Variable

[ Edited ]

If you have a NUMERIC variable that you want to display with leading zeroes you do not need to create a text variable you can use a Z format to provide them.

 

or possibly what you want is

data one;

   array c{25} $;

   do i=0 to 24;

      c[i] = put (i, z2.);

      output;

    end;

    drop i;

run;

Contributor
Posts: 42

Re: Convert do loop sequence to Char Variable

Thanks a lot for your information ..It worked properly Smiley Happy

Super User
Posts: 6,926

Re: Convert do loop sequence to Char Variable

The iteration variable from the do statement is always numeric, so either use a Zw. format for display, or do

data want (keep=i);
do i1 = 1 to 24;
  i = put(i1,z2.);
  output;
end;
run;

to get a character variable in your dataset.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 10,454

Re: Convert do loop sequence to Char Variable

Minor correct to @KurtBremser, an iterated do loop will accept an explicit list of values separated by commas but will not all the From - To-(and especially By).

data junk;
   do c='00','01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24';
      output;
   end;
run;
Solution
‎02-17-2017 11:15 AM
Contributor
Posts: 42

Re: Convert do loop sequence to Char Variable

Thanks a lot ..It worked Smiley Happy

Contributor
Posts: 42

Re: Convert do loop sequence to Char Variable

Thanks a lot it worked Smiley Happy

☑ This topic is SOLVED.

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

Discussion stats
  • 6 replies
  • 173 views
  • 2 likes
  • 3 in conversation