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;
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.
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
Thank you I got it.
You have three main issues, one conceptual, two syntax issues.
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.
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.
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!
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.