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

Hi, I am soon taking the Advanced Programmer exam but am stuck on one of the sample questions:

 

Write a SAS program that will:
• Create output data set work.ACT01 using sashelp.pricedata as input.
• Use an array to increase the values of the price1 through price17 variables by 10%.

 

Arrays and do loops would be used in the program.

 

I keep getting the subscript out of range error with this attempt:

 

data work.ACT01;
set sashelp.pricedata;
array original {17} price1-price17;

do i=1 - 17;
original{i} = original{i}*1.1;

end;

run;

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

do i = 1 to 17;

The - would be taken as subtraction. So Do i= -16 would be the first value of i and your array is not defined to have negative indices.

 

If you don't believe me place a PUT i; statement inside the do loop.

View solution in original post

6 REPLIES 6
ballardw
Super User

do i = 1 to 17;

The - would be taken as subtraction. So Do i= -16 would be the first value of i and your array is not defined to have negative indices.

 

If you don't believe me place a PUT i; statement inside the do loop.

agwah
Calcite | Level 5
Oh wow, that was driving me crazy for a while. Thank you!
Smartrp21
Calcite | Level 5

data test;
set sashelp.pricedata;
array oldprice[17] price1-price17;
array newprice[17] price1-price17;
do i=1 to 17;
newprice[i]=oldprice[i]*1.1;
end;
drop i;
run;

Chizoba
Obsidian | Level 7

Hi, below is my solution. I hope it helps.

 

DATA work.ACT01 (drop=i);
SET sashelp.pricedata end=lastrow;
ARRAY prc[17] price1-price17;
ARRAY prc_incr[17] ;
do i=1 to 17;
prc_incr[i] = prc[i]*1.1;
end;
run;

#You can do a proc print to check the first 10 observations.
proc print data=work.ACT01 (obs=10); run;

Tom
Super User Tom
Super User

When answering ambiguous questions on exams it is best to add text that clarifies how you have interpreted the question.

 

Your interpretations seems to be that you want to create a new array of variables to hold the increased values.  

 

Another valid interpretation is that they want you to update the existing variables' values.

* Assuming the request is to update the existing variables ;
DATA work.ACT01  ;
  SET sashelp.pricedata ;
  ARRAY price[17] ;
  do i=1 to 17;
     price[i] = price[i]*1.1;
  end;
  drop i;
run;

 

Chizoba
Obsidian | Level 7
Thanks! I will definitely take note of this (incorporating texts for clarification) in subsequent exams.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 2675 views
  • 9 likes
  • 5 in conversation