The following code works as expected.
data have;
set have;
by veh_no;
if veh_no="&num" then
do;
if var1 ne '0' then
do;
if first.var1 then
cusumvar1=0;
cusumvar1+1;
end;
end;
run;
However, when I add a similar if then loop I get an error message.
ERROR 22-322: Syntax error, expecting one of the following: (, +, =.
data have;
set have;
by veh_no;
if veh_no="&num" then
do;
if var1 ne '0' then
do;
if first.var1 then
cusumvar1=0;
cusumvar1+1;
end;
if var2 ne '0' then
do;
if first.var2 then
cusumvar2=0;
cusumvar2+1;
end;
end;
run;
A few notes might help push you in the right direction ...
First, don't use DATA HAVE; SET HAVE; when you are experimenting and trying to get a program to work. If an experiment fails (but doesn't generate an error message), you will have replaced your data with a poor substitute.
Second, either you didn't show the code you ran, or your expectations are bizarre. There is no way the top program ran "as expected" when you include this combination of statements:
BY VEH_NO;
IF FIRST.VAR1 THEN ...
The program doesn't create FIRST.VAR1. At a minimum, the log should give you a message to that effect.
Finally, is this code part of a macro? If not, you should be able to pinpoint in the log which statement the error message refers to.
When debugging programs with macro-variables always turn on options mprint and symbolgen. Posting the log will make debugging easier, because without your data we are unable to reproduce the problem you have.
There is an error in your first data-step, you can't use
first.var1
without having var1 in the BY-statement.
The if-then-statement is not a loop 😉
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.