DATA Step, Macro, Functions and more

if then loop within data

Reply
Contributor
Posts: 52

if then loop within data

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;
Super User
Posts: 5,513

Re: if then loop within data

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.

Super Contributor
Posts: 345

Re: if then loop within data

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 Smiley Wink

 

Ask a Question
Discussion stats
  • 2 replies
  • 77 views
  • 2 likes
  • 3 in conversation