Do loop with conditions

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 77
Accepted Solution

Do loop with conditions

I wish to call a macro if a condition is met. The two options I see are in the following code snippets, however, I don't know how to do either. The first updates a table that is internal to the do loop.

data _null_;
	array veh_no[1006] _temporary_ (2800:3805)
	;
	call execute('proc sql;');

	do i=1 to dim(veh_no) while (veh_no(i) ne .);
		if <daily update table condition> then
			do:
			call execute(cats('%nrstr(%rept_map)(',veh_no(i),')'));
			end;
	end;

	call execute('quit;');
run;

OR the second uses the same updated table to update the do loop array. 

data _null_;
	array veh_no[varying] _temporary_ <input from updated table have>
	;
	call execute('proc sql;');

	do i=1 to dim(veh_no) while (veh_no(i) ne .);
		call execute(cats('%nrstr(%rept_map)(',veh_no(i),')'));
	end;

	call execute('quit;');
run;

Accepted Solutions
Solution
‎12-18-2017 03:31 PM
Frequent Contributor
Posts: 77

Re: Do loop with conditions

Thanks Tom,

 

I was able to get the 2nd option to work more or less as below.

 

In creating 'have' i used 'where' in the context of create table to set up the column 'Unit'. The array 'veh_no' is then assigned the values of Unit.

 

Table 'have' is updated on a daily basis. 

 

If you can think of any land mines I may have placed in the logic I'd appreciate ideas.

 

Thanks

data _null_;
        set have;
	array veh_no Unit
	;
	call execute('proc sql;');

	do i=1 to dim(veh_no) while (veh_no(i) ne .);
		call execute(cats('%nrstr(%rept_map)(',veh_no(i),')'));
	end;

	call execute('quit;');
run;

View solution in original post


All Replies
Super User
Posts: 12,148

Re: Do loop with conditions

Do you have syntax where you tested this without any conditions? Such as with a single veh_no value?

Proc sql;

%rept_map(,2800,) ;

quit;

for example as that is apparently one of the calls you are attempting.

 

If you have run the code and gotten errors then 1) run the code after setting options mprint symbolgen; and 2) Paste the log with the coe and the errors (for only one or two veh_no values please) into a code box opened with the forum menu icon {I}.

 

I think you have some syntax issues such as commas in your macro call and a very likely unneeded %nrstr to complicate things.

 

You might show the syntax for your %rept_map macro as well.

Frequent Contributor
Posts: 77

Re: Do loop with conditions

Hi,

 

The macro is fine. I don't know how to reference the conditions for the looping.

 

Thanks.

Frequent Contributor
Posts: 77

Re: Do loop with conditions

Sorry for the confusion. I don't know how to create a variable array that is updated from a table. I also don't know how to insert the variable table into the loop as a condition. 

Trusted Advisor
Posts: 1,683

Re: Do loop with conditions


capam wrote:

Sorry for the confusion. I don't know how to create a variable array that is updated from a table. I also don't know how to insert the variable table into the loop as a condition. 


Can you post example of your table? 

What do you want to deliver to the loop in the macro ? Is it the veh_no array values ?

What do you mean by: " insert the variable table into the loop as a condition. " ? give an example.

 

Super User
Super User
Posts: 7,388

Re: Do loop with conditions

[ Edited ]

What is the CONDITION you are trying to test?

In the first data step you posted the only values you seem to have are the integers from 2,800: to 3,805 that you used as initial values in your array.  What is it that you are trying to test in your IF statement?  Is it the value of some variable? If so what variable? From what dataset?

 

It kind of sounds like you have a dataset with a list of updates.  Say this is named DAILY_UPDATE_TABLE and it has the variable VEH_NO in it.  I like to generate code to a text file instead of using CALL EXECUTE because it is easier to debug. It is also clearer to novice users that the generate code will run AFTER the data step finishes.

 

filename code temp;
data _null_;
  set DAILY_UPDATE_TABLE ;
  file code ;
  put '%rept_map(' veh_no ');' ;
run;

%include code / source2;

If there is some other variable in your source dataset that you need to test before generating the macro call then add that test the data step.  Perhaps as a WHERE statement of a subsetting IF statement.

 

If the macro calls generate only PROC SQL statement and need to be wrapped inserted between PROC SQL; and QUIT; statement then just add those around the %INCLUDE statement.

 

Solution
‎12-18-2017 03:31 PM
Frequent Contributor
Posts: 77

Re: Do loop with conditions

Thanks Tom,

 

I was able to get the 2nd option to work more or less as below.

 

In creating 'have' i used 'where' in the context of create table to set up the column 'Unit'. The array 'veh_no' is then assigned the values of Unit.

 

Table 'have' is updated on a daily basis. 

 

If you can think of any land mines I may have placed in the logic I'd appreciate ideas.

 

Thanks

data _null_;
        set have;
	array veh_no Unit
	;
	call execute('proc sql;');

	do i=1 to dim(veh_no) while (veh_no(i) ne .);
		call execute(cats('%nrstr(%rept_map)(',veh_no(i),')'));
	end;

	call execute('quit;');
run;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 225 views
  • 0 likes
  • 4 in conversation