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

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

Accepted Solutions
capam
Pyrite | Level 9

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

6 REPLIES 6
ballardw
Super User

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.

capam
Pyrite | Level 9

Hi,

 

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

 

Thanks.

capam
Pyrite | Level 9

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. 

Shmuel
Garnet | Level 18

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

 

Tom
Super User Tom
Super User

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.

 

capam
Pyrite | Level 9

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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 6 replies
  • 839 views
  • 0 likes
  • 4 in conversation