BookmarkSubscribeRSS Feed
knveraraju91
Barite | Level 11

Dear,

 

 I need help in second data step where I created variable (CW). The array reference is not working. How to give an array reference depends on number of responses. Thank you

 

output needed:

 dt1     response1 dt2       response2  dt3         response3   dt4        response4   dt5        response5     dt6          response       

21109    PR          21171     PR             21227    PR              21290     PR             21350     CR             21433           CR

 

 

 

data one;
input a $1-26 date $27-80; datalines; PR PR PR PR CR CR 21109 21171 21227 21290 21350 21433 PR CR CR CR CR CR CR CR CR 20803 20853 20915 20978 21041 21105 21188 21279 21357 CR CR CR CR CR 21119 21187 21243 21312 21370 CR CR PR 21272 21329 21384 ; data two; set one; cw = countw(a); array dt{*} ; array response{*}; do i=1 to no_of_cw; if scan(a,i) in ('CR' 'PR') then dt(i)=scan(date,i); response(i)=scan(a,i); end; run;

   

5 REPLIES 5
Kurt_Bremser
Super User

no_of_cw is uninitialized, use cw.

The arrays need to be defined with an explicit number of elements. Select a sufficient number according to the maximum elements in a and date.

knveraraju91
Barite | Level 11
data two;
set one;
cw = countw(a);
array dt{cw};
array response{cw};
do i=1 to cw;
if scan(a,i) in ('CR' 'PR') then  dt(i)=scan(date,i);
response(i)=scan(a,i);
end;
run;

I just tried this. This is not working. Thank you for reply. Please 

knveraraju91
Barite | Level 11

Thank you I got it.

 

 

Reeza
Super User

You have three main issues, one conceptual, two syntax issues.

  • You cannot have an array reference size that changes for each line. Therefore your array reference size needs to be as large as possible to hold all your data. Since you have 1 to 26 (27 spaces) and each character is 2+space, you have 27/3, you would have 9 maximum. Or whatever your math works out to be. You can control the loop counter to only fill that information in, but the array declaration and size needs to be predetermined.
  • There is a variable, no_of_cw, that is not created anywhere. There should be a note in your log to that effect.
  • You also need to specify the type of your array ie characters. If you want to convert it to numeric, declare it as numeric, but you do need to convert it as well.
data two;
set one;
*Number of items in list;
cw = countw(a);

 
array dt{9} $;
array response{9} $;

do i=1 to cw;
if scan(a,i) in ('CR' 'PR') then  dt(i)=scan(date,i);
response(i)=scan(a,i);
end;


run;

Pretty sure I've mentioned this before, but if you comment your code it really helps. Both us and you, because then we understand what you're trying to do and from a personal standpoint, I find if I understand the process enough to write comments, I usually avoid a lot of trivial errors like referencing the wrong variable and such. 

 

EDIT: fix reference vs size for clarity.

 

 

 

ballardw
Super User

You are attempting to put character values CR PR into a numeric defined array response.

As was mentioned arrays must have a declared size or explicit list of variables to set membership.

If you don't want "character converted to numeric" messages in the log use input.

 

data two;
   set one;
   array dt{9} ;
   array response{9} $ ;
   cw=countw(a);
   do i=1 to cw ;
      if scan(a,i) in ('CR' 'PR') then  dt(i)=input(scan(date,i),best.);
      response(i)=scan(a,i);
   end;
run;

And if those are actually dates in the dt array then you might consider assigning an actual date format to the dt variables.

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 875 views
  • 2 likes
  • 4 in conversation