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

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;
1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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])

 

 

 

--
Paige Miller

View solution in original post

10 REPLIES 10
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
Demographer
Pyrite | Level 9

Hi,

I'm creating my own data.

The log complete log message is the following:

 

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;
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
Demographer
Pyrite | Level 9
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;
PaigeMiller
Diamond | Level 26

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])

 

 

 

--
Paige Miller
Jagadishkatam
Amethyst | Level 16

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;
Thanks,
Jag
Demographer
Pyrite | Level 9
No. For i=4, I want for instance the sum of prob1+prob2+prob3. In your solution, it just sums up prob1+prob3.
Jagadishkatam
Amethyst | Level 16

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;
Thanks,
Jag
Kurt_Bremser
Super User

@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.

Kurt_Bremser
Super User

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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 1876 views
  • 1 like
  • 4 in conversation