Hi,
I'm trying to sum variables prob1 to prob[i-1] from an array. For some reasons, SAS doesn't recognize that it's a range of variable, so I got an error message saying that a , or ) is expected. What should I do?
data test2;
array piM {52} pi1-pi52;
array prob {52} prob1-prob52;
do i=1 to 52;
piM[i]=0.5*pdf('normal',i,26,10);
if i=1 then prob[i]=pim[i]; if i>1 then prob[i]=piM[i]/(1-sum(of prob[1]-prob[i-1]));
end;
run;
Thanks but I wanted the log from SAS pasted into the </> window, not the log (which is not properly formatted) from your earlier positing pasted into the </> window.
Then you will see this, which exactly pinpoints the problem
1 data test2; 2 3 array piM {52} pi1-pi52; 4 array prob {52} prob1-prob52; 5 do i=1 to 52; 6 piM[i]=0.5*pdf('normal',i,26,10); 7 if i=1 then prob[i]=pim[i]; if i>2 then prob[i]=piM[i]/(1-sum(of prob[1]-prob[i-1])); - 22 ERROR 22-322: Syntax error, expecting one of the following: ), ','. 8 end; 9 run;
You can't use subtraction with sum(of ...)
You probably want:
sum(prob[1],-prob[i-1])
Show us the log. Include the entire data step code and NOTES, WARNINGS and ERRORs. Do not chop anything out of the log.
When providing the log, it is critical that you maintain the formatting of the log so we can see it exactly as SAS showed it to you, making it easier for us to use. To maintain the formatting of the log, click on the </> icon and paste the log as text into the window that appears. DO NOT SKIP THIS STEP.
Also, for future reference, saying there is an error message but not showing it to us just slows you down, because we will ask to see the error message.
Hi,
I'm creating my own data.
The log complete log message is the following:
You didn't follow the instructions to show us the log, and so it is not very interpretable the way you showed it. Please show us the log by clicking on the </> icon.
1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK; 72 73 data test2; 74 75 array piM {52} pi1-pi52; 76 array prob {52} prob1-prob52; 77 do i=1 to 52; 78 piM[i]=0.5*pdf('normal',i,26,10); 79 if i=1 then prob[i]=pim[i]; if i>2 then prob[i]=piM[i]/(1-sum(of prob[1]-prob[i-1])); _ 22 ERROR 22-322: Syntax error, expecting one of the following: ), ','. 80 end; 81 run;
Thanks but I wanted the log from SAS pasted into the </> window, not the log (which is not properly formatted) from your earlier positing pasted into the </> window.
Then you will see this, which exactly pinpoints the problem
1 data test2; 2 3 array piM {52} pi1-pi52; 4 array prob {52} prob1-prob52; 5 do i=1 to 52; 6 piM[i]=0.5*pdf('normal',i,26,10); 7 if i=1 then prob[i]=pim[i]; if i>2 then prob[i]=piM[i]/(1-sum(of prob[1]-prob[i-1])); - 22 ERROR 22-322: Syntax error, expecting one of the following: ), ','. 8 end; 9 run;
You can't use subtraction with sum(of ...)
You probably want:
sum(prob[1],-prob[i-1])
please try the below code, instead of using sum(of ) changed to sum(prob1,prob(i-1)
data test2;
array piM {52} pi1-pi52;
array prob {52} prob1-prob52;
do i=1 to 52;
piM[i]=0.5*pdf('normal',i,26,10);
if i=1 then prob[i]=pim[i];
if i>1 then prob[i]=piM[i]/(1-sum(prob1,prob(i-1)));
end;
run;
Please try the updated code
data test2;
array piM {52} pi1-pi52;
array prob {52} prob1-prob52;
do i=1 to 52;
piM[i]=0.5*pdf('normal',i,26,10);
if i=1 then prob[i]=pim[i];
if i>1 then prob[i]=piM[i]/(1-sum(of prob(i-1)));
end;
run;
@Demographer wrote:
No. For i=4, I want for instance the sum of prob1+prob2+prob3. In your solution, it just sums up prob1+prob3.
This is not what you accepted as solution. The accepted solution results in prob1 - prob3.
With "of", you either need to use a comprehensive list of elements, or the whole array; you cannot address a subset of the array as you intended.
Keep a running sum of your prob array:
data test2;
array pim {52} pi1-pi52;
array prob {52} prob1-prob52;
sumprob = 0;
do i = 1 to 52;
pim[i] = 0.5 * pdf('normal',i,26,10);
if i = 1
then prob[i] = pim[i];
else prob[i] = pim[i] / (1 - sumprob);
sumprob = sum(sumprob,prob[i]);
end;
run;
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.