Array Output not working as expected

Accepted Solution Solved
Reply
Contributor
Posts: 39
Accepted Solution

Array Output not working as expected

[ Edited ]

Hello,

 

I have the code below.

 

data want;

set have;

    array q q3a q3b q4a q4b;
    array q_new q3a_par q3b_par q4a_par q4b_par;
    do i=1 to dim(q);
        if q(i)=1 then q_new(i)="01";
        else if q(i)=2 then q_new(i)="02";
        else if q(i)=. then q_new(i)="";
    end;

run;

 

The I want the variable created by array q_new to be character, but currently it is just keeping it as numeric. Is there anything syntax wise that I am missing?

 

For reference. The current data being outputted is:

 

q3a q3b q4a q4b q3a_par q3b_par q4a_par q4b_par

1      2       1              1              2           1                   

 

 

Thank You.


Accepted Solutions
Solution
‎01-18-2017 02:14 PM
Super User
Posts: 5,362

Re: Array Output not working as expected

Yes, it's the ARRAY statement that is creating the new variables.  By default, they are created as numeric.  It's easy to change that:

 

array q_new {4} $ 2 q3a_par q3b_par q4a_par q4b_par;

View solution in original post


All Replies
Solution
‎01-18-2017 02:14 PM
Super User
Posts: 5,362

Re: Array Output not working as expected

Yes, it's the ARRAY statement that is creating the new variables.  By default, they are created as numeric.  It's easy to change that:

 

array q_new {4} $ 2 q3a_par q3b_par q4a_par q4b_par;

Contributor
Posts: 39

Re: Array Output not working as expected

Thank you.
Super User
Posts: 11,121

Re: Array Output not working as expected

I have to assume the variables represented by q_new are being created in the array declaration. So you need to specify them as character

 

array q_new {4} $5 q3a_par q3b_par q4a_par q4b_par;

creates 4 character varaibles or length 5

 

I might suggest:

q_new[i] = put(p[i],z2.);

for your explicit example.

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 182 views
  • 1 like
  • 3 in conversation