BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
superbug
Quartz | Level 8

I want to transpose data from long back to wide. The following is my code, but the error message is

" ERROR: Array subscript out of range at line 7276 column 1"

Please help. Thank you!

 

proc sort data=&examlevel._exam_long_1;
by formname testdate examscoreid itemscor itemname;
run;

 

%let nitem=50;

 

7246 data &examlevel._exam_valid_only;
7247 array iname(&nitem) $16.;
7248 array iscor(&nitem) $;
7249 array ikey(&nitem);
7250 array iresp(&nitem);
7251 array iraw(&nitem);
7252 array ires(&nitem) $;
7253 array isec(&nitem);
7254 array idur(&nitem);
7255 array itype(&nitem) $;
7256 array istat(&nitem);
7257 array iseq(&nitem);
7258 array irubric(&nitem) $;
7259 array icontentarea(&nitem) $;
7260 array idiagnostic(&nitem) $;
7261 array icognitive(&nitem);
7262 array iadj_pbis(&nitem);
7263 array ip_value(&nitem);
7264 array idifficulty(&nitem);
7265 array itimesScored(&nitem);
7266 array itimesScoredAlternate(&nitem);
7267 array itimespretest(&nitem);
7268 array itimesPretestedAlternate(&nitem);
7269
7270
7271 do i=1 by 1 until (last.examscoreid);
7272
7273 set &examlevel._exam_long_1;
7274
7275 by formname testdate examscoreid itemscor itemname;
7276 iname(i)=itemname;
7277 iscor(i)=itemscor;
7278 ikey(i)=itemkey;
7279 iresp(i)=itemresp;
7280 iraw(i)=itemraw;
7281 ires(i)=itemres;
7282 isec(i)=itemsec;
7283 idur(i)=itemdur;
7284 itype(i)=itemtype;
7285 istat(i)=itemstat;
7286 iseq(i)=itemseq;
7287 irubric(i)=rubric;
7288 icontentarea(i)=contentarea;
7289 idiagnostic(i)=diagnostic;
7290 icognitive(i)=cognitive;
7291 iadj_pbis(i)=adj_pbis;
7292 ip_value(i)=p_value;
7293 idifficulty(i)=difficulty;
7294 itimesScored(i)=scored_standard;
7295 itimesScoredAlternate(i)=scored_alternate;
7296 /*timesScoredPEAT(i)=__TimesScoredPEAT;*/
7297 itimesPretest(i)=pretest_standard;
7298 itimesPretestedAlternate(i)=pretest_alternate;
7299 ikey(i)=key;
7300 end;
7301 drop itemname itemscor itemkey itemresp itemraw itemres itemsec itemdur itemtype itemstat
7301! itemseq
7302 rubric contentarea diagnostic cognitive adj_pbis p_value difficulty scored_standard
7302! scored_alternate
7303 pretest_standard pretest_alternate key;
7304 run;

NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).
7290:1
ERROR: Array subscript out of range at line 7276 column 1.
iname1=JAL000002 iname2=JAL000002 iname3=JAL000002 iname4=JAL000007 iname5=JAL000007 iname6=JAL000007
iname7=JAL000011 iname8=JAL000011 iname9=JAL000011 iname10=JAL000021 iname11=JAL000021
iname12=JAL000021 iname13=JAL000027 iname14=JAL000027 iname15=JAL000027 iname16=JAL000028
iname17=JAL000028 iname18=JAL000028 iname19=JAL000029 iname20=JAL000029 iname21=JAL000029
iname22=JAL000032 iname23=JAL000032 iname24=JAL000032 iname25=JAL000034 iname26=JAL000034
iname27=JAL000034 iname28=JAL000036 iname29=JAL000036 iname30=JAL000036 iname31=JAL000037
iname32=JAL000037 iname33=JAL000037 iname34=JAL000039 iname35=JAL000039 iname36=JAL000039
iname37=JAL000041 iname38=JAL000041 iname39=JAL000041 iname40=JAL000046 iname41=JAL000046
iname42=JAL000046 iname43=JAL000048 iname44=JAL000048 iname45=JAL000048 iname46=JAL000051
iname47=JAL000051 iname48=JAL000051 iname49=JAL000052 iname50=JAL000052 iscor1=1 iscor2=1 iscor3=1
iscor4=1 iscor5=1 iscor6=1 iscor7=1 iscor8=1 iscor9=1 iscor10=1 iscor11=1 iscor12=1 iscor13=1
iscor14=1 iscor15=1 iscor16=1 iscor17=1 iscor18=1 iscor19=1 iscor20=1 iscor21=1 iscor22=1 iscor23=1
iscor24=1 iscor25=1 iscor26=1 iscor27=1 iscor28=1 iscor29=1 iscor30=1 iscor31=1 iscor32=1 iscor33=1
iscor34=1 iscor35=1 iscor36=1 iscor37=1 iscor38=1 iscor39=1 iscor40=1 iscor41=1 iscor42=1 iscor43=1
iscor44=1 iscor45=1 iscor46=1 iscor47=1 iscor48=1 iscor49=1 iscor50=1 ikey1=3 ikey2=3 ikey3=3 ikey4=3
ikey5=3 ikey6=3 ikey7=3 ikey8=3 ikey9=3 ikey10=4 ikey11=4 ikey12=4 ikey13=3 ikey14=3 ikey15=3 ikey16=3
ikey17=3 ikey18=3 ikey19=2 ikey20=2 ikey21=2 ikey22=4 ikey23=4 ikey24=4 ikey25=2 ikey26=2 ikey27=2
ikey28=4 ikey29=4 ikey30=4 ikey31=2 ikey32=2 ikey33=2 ikey34=4 ikey35=4 ikey36=4 ikey37=1 ikey38=1
ikey39=1 ikey40=3 ikey41=3 ikey42=3 ikey43=1 ikey44=1 ikey45=1 ikey46=3 ikey47=3 ikey48=3 ikey49=4
ikey50=4 iresp1=3 iresp2=3 iresp3=3 iresp4=3 iresp5=3 iresp6=3 iresp7=3 iresp8=3 iresp9=3 iresp10=2
iresp11=2 iresp12=2 iresp13=3 iresp14=3 iresp15=3 iresp16=3 iresp17=3 iresp18=3 iresp19=2 iresp20=2

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Hi @superbug  Instead of random %let nitem=50;

 

Try the below before your datastep
proc sql;
select max(c) into :nitem trimmed
from (select count(*) as c from &examlevel._exam_long_1
group by formname, testdate ,examscoreid);
quit;

%put &=nitem;

View solution in original post

10 REPLIES 10
ChrisNZ
Tourmaline | Level 20

Further down the log where all the variable values are listed, you'll find i=51

This value is greater than the size of your array.

superbug
Quartz | Level 8

@ChrisNZ 

Thanks for your reply. I am seeing the last is iname50=JAL000052, where is i=51?

Kurt_Bremser
Super User

@superbug wrote:

@ChrisNZ 

Thanks for your reply. I am seeing the last is iname50=JAL000052, where is i=51?


MUCH further down. SAS needs to list ALL members of ALL arrays before it will show the variable i.

 

What are you trying to accomplish with this transpose? And how do you calculate the macro variable?

superbug
Quartz | Level 8

@Kurt_Bremser @novinosrin 

 

I have a bunch of test forms, each form has 50 items.

I got "&examlevel._exam_long" data by transpose a wide data using the following code. 

 

%let nitem=50;

data &examlevel._exam_long;
set &examlevel._exam2;
by  formname testdate examscoreid;
array names(1:&nitem) $16. iname1 - iname&nitem;
array scors(1:&nitem) $10. iscor1 - iscor&nitem;
array keys(1:&nitem) ikey1  - ikey&nitem;
array resps(1:&nitem) iresp1 - iresp&nitem;
array raws(1:&nitem) iraw1 - iraw&nitem;
array ress(1:&nitem) $30. ires1 - ires&nitem;
array secs(1:&nitem) isec1 - isec&nitem;
array durs(1:&nitem) idur1 - idur&nitem;
array types(1:&nitem)$20. itype1 - itype&nitem;
array stats(1:&nitem) istat1 - istat&nitem;
array seqs(1:&nitem) iseq1 - iseq&nitem;
do j=1 by 1 to dim(scors);
itemname=names(j);
itemscor=scors(j);
itemkey=keys(j);
itemresp=resps(j);
itemraw=raws(j);
itemres=ress(j);
itemsec=secs(j);
itemdur=durs(j);
itemtype=types(j);
itemstat=stats(j);
itemseq=seqs(j);
output;
end;
drop iname1-iname&nitem iscor1-iscor&nitem
ikey1-ikey&nitem iresp1-iresp&nitem iraw1-iraw&nitem 
ires1-ires&nitem isec1-isec&nitem idur1-idur&nitem 
itype1-itype&nitem istat1-istat&nitem iseq1-iseq&nitem
irest1-irest&nitem irept1-irept&nitem iwgt1-iwgt&nitem
ivers1-ivers&nitem idist1-idist&nitem;
run;

I added a few more variables to the "&examlevel._exam_long" data by merging it with another data, I called the merged data as  "&examlevel._exam_long_1" 

Then I use the the following code to transpose "&examlevel._exam_long_1" data back to wide. Why there is error message of "ERROR: Array subscript out of range at line 7276 column 1."  Please see the highlighted line below, which is line 7276 .

 

data &examlevel._exam_valid_only;
array iname(&nitem) $16.; 
array iscor(&nitem) $;
array ikey(&nitem);
array iresp(&nitem);
array iraw(&nitem);
array ires(&nitem) $;
array isec(&nitem);
array idur(&nitem);
array itype(&nitem) $;
array istat(&nitem);
array iseq(&nitem);
array irubric(&nitem) $;
array icontentarea(&nitem) $;
array idiagnostic(&nitem) $;
array icognitive(&nitem);
array iadj_pbis(&nitem);
array ip_value(&nitem);
array idifficulty(&nitem);
array itimesScored(&nitem);
array itimesScoredAlternate(&nitem);
array itimespretest(&nitem);
array itimesPretestedAlternate(&nitem);

do i=1 by 1 until (last.examscoreid);

set &examlevel._exam_long_1;

by formname testdate examscoreid itemscor itemname;
iname(i)=itemname;  /* this is line 7276, error message is here*/
iscor(i)=itemscor;
ikey(i)=itemkey;
iresp(i)=itemresp;
iraw(i)=itemraw;
ires(i)=itemres;
isec(i)=itemsec;
idur(i)=itemdur;
itype(i)=itemtype;
istat(i)=itemstat;
iseq(i)=itemseq;
irubric(i)=rubric;
icontentarea(i)=contentarea;
idiagnostic(i)=diagnostic;
icognitive(i)=cognitive;
iadj_pbis(i)=adj_pbis;
ip_value(i)=p_value;
idifficulty(i)=difficulty;
itimesScored(i)=scored_standard;
itimesScoredAlternate(i)=scored_alternate;
itimesPretest(i)=pretest_standard;
itimesPretestedAlternate(i)=pretest_alternate;
ikey(i)=key;
end;
drop itemname itemscor itemkey itemresp itemraw itemres itemsec itemdur itemtype itemstat itemseq
rubric contentarea diagnostic cognitive adj_pbis p_value difficulty scored_standard scored_alternate
pretest_standard pretest_alternate key;
run;

 

novinosrin
Tourmaline | Level 20

Please check the count of records for each group by for validation of the 1st transposed one i.e. wide to long. See what's transposed. Then if each group by contains the same count, you are good. 

 

Then you can attempt -LONG to WIDE. Please do it in piecemeal.

superbug
Quartz | Level 8

@novinosrin 

Thank you for the suggestion!

I checked the first transpose data, the n count for each by group is the same, that is, 50.

As a check, now I use the following code to transpose this long data back to wide (without merging with the other data), but there is still error message of "ERROR: Array subscript out of range at line 728 column 1". Could you please let me know what's wrong with the code below? Thanks!

data long;
set &examlevel._exam_long;
proc sort; by formname testdate examscoreid;
run;

data wide; array iname(&nitem) $16.; array iscor(&nitem) $; array ikey(&nitem); array iresp(&nitem); array iraw(&nitem); array ires(&nitem) $; array isec(&nitem); array idur(&nitem); array itype(&nitem) $; array istat(&nitem); array iseq(&nitem); do i=1 by 1 until (last.examscoreid); set long; by formname testdate examscoreid; iname(i)=itemname; /* this is line 728, the error message line */ iscor(i)=itemscor; ikey(i)=itemkey; iresp(i)=itemresp; iraw(i)=itemraw; ires(i)=itemres; isec(i)=itemsec; idur(i)=itemdur; itype(i)=itemtype; istat(i)=itemstat; iseq(i)=itemseq; end; run;

"

 

novinosrin
Tourmaline | Level 20

The log should have a note of the value of itemname and I index value at the very point at which it tripped beyond the array range. Did you notice that in the full log? That would confirm whether or not the count is 50 or higher

superbug
Quartz | Level 8

@novinosrin 

It turned out there are some errors in the data. I figured out how to fix it.

A big thumb up for your expertise and help. Thank you so much!

novinosrin
Tourmaline | Level 20

Hi @superbug  Instead of random %let nitem=50;

 

Try the below before your datastep
proc sql;
select max(c) into :nitem trimmed
from (select count(*) as c from &examlevel._exam_long_1
group by formname, testdate ,examscoreid);
quit;

%put &=nitem;

superbug
Quartz | Level 8

@novinosrin Thanks much for your suggestion! using your code, it turned out n=280.  Using n=280, I can ran through the code without error. But the resulted matrix is a sparse matrix.

the &examlevel._exam_long_1 data is a stack of multiple test forms. Each form has n=50 items. that's why I used n=50 in the code. There are common items across some forms.  I think I need to limit &examlevel._exam_long_1 to one specific form only. 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 10 replies
  • 940 views
  • 4 likes
  • 4 in conversation