DATA Step, Macro, Functions and more

Use var from have in do loop

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 78
Accepted Solution

Use var from have in do loop

I'm attempting to access a variable in have for calling a macro from a do loop. When I try the following code I get errors:

 

ERROR: The DIM, LBOUND, and HBOUND functions require an array name for the first argument.

ERROR 68-185: The function Var_in_have is unknown, or cannot be accessed.

data have;
	call execute('proc sql;');

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

	call execute('quit;');
run;

Accepted Solutions
Solution
‎08-17-2017 05:06 PM
Super User
Posts: 6,901

Re: Use var from have in do loop

Yes, you will need the code from the wizard, so you can plug it into a program.  If you are looking to get a separate plot for each combination of values that VEHNO and FAULTNO take on, it sounds like you need this:

 

proc sort data=have;

by VehNo FaultNo;

run;

 

proc plot /* or sgplot or whatever the wizard tells  you to use */ data=have;

by VehNo FaultNo;

*** Add the rest of the code from the wizard;

run;

 

 

View solution in original post


All Replies
Super User
Posts: 23,950

Re: Use var from have in do loop

You don't have a SET statement so you aren't using any data. 

 

Build your string outside of CALL EXECUTE and check what it's generating first. Then pass the string to CALL EXECUTE. 

 

In my experience, you don't usually need the %nrstr() when using CALL EXECUTE unless there's something you need to mask. 



Super User
Posts: 6,901

Re: Use var from have in do loop

There are bright red DANGER signs throughout your post that indicate you should not be doing this.

 

You need to understand SAS language for macro language to be useful.  Macro language does not process your data.  It builds a program by generating working SAS statements.  You are not ready for this yet.  Here are just a few examples.

 

Your message indicates that you think you are using data from the data set HAVE.  You're not.  There is nothing about your program that reads from an existing SAS data set.

 

You use the DIM function to refer to an array named Var_in_have.  There is no such array.  If you did have such an array, it would seem that it should contain names of variables.  The reference to "ne ." would be incorrect for referring to a missing value for the name of a variable.

 

Your intent seems to be to run a macro for every variable.  While that's possible, it's extremely unlikely that the same macro would be useful for both character and numeric variables.

 

You would do better to describe your original data and what you are trying to accomplish.  You could get answers here, possibly very simple answers.  The answers may or may not include macro language (depending on the goal).

Frequent Contributor
Posts: 78

Re: Use var from have in do loop

Posted in reply to Astounding

Hi Astounding,

 

Thanks for your comments. The problem statement has changed since original posting.

 

Problem statement: generate series of line plots from 4 variables.

Source: have is 1 table with the 4 variables of interest:

VehNo is integer

FaultNo is integer

stat is float

date is dt format.

 

Output will be line plots of stat vs date.

 

Typical loop that calls a line plot macro follows:

 

for i = 1:dim(VehNo)

   for j = 1:dim(FaultNo)

        call line_plot_macro(VehNo(i),FaultNo(j))

   end j

end i

 

The macro will access the data. The macro will organize and plot the stats by occurance date from the instances of a specific fault for a specific vehicle.

 

Initially there should be in the neighborhood of 100 plots which will increase as data is collected. 

 

I can probably get the line plot macro by modifying code generated by the line plot wizard.

 

I hope this is clearer.

 

capam

 

Solution
‎08-17-2017 05:06 PM
Super User
Posts: 6,901

Re: Use var from have in do loop

Yes, you will need the code from the wizard, so you can plug it into a program.  If you are looking to get a separate plot for each combination of values that VEHNO and FAULTNO take on, it sounds like you need this:

 

proc sort data=have;

by VehNo FaultNo;

run;

 

proc plot /* or sgplot or whatever the wizard tells  you to use */ data=have;

by VehNo FaultNo;

*** Add the rest of the code from the wizard;

run;

 

 

Super User
Posts: 13,886

Re: Use var from have in do loop

You may want to look into a completely different approach the Graph Templat Language let's you do some pretty interesting things and I think it is much more straight forward then what it looks like you are attempting

 

http://support.sas.com/kb/41/367.html

http://support.sas.com/kb/39/099.html

http://support.sas.com/kb/39/092.html

 

All show different groupings of series charts. Go the Results tabl on the page to see the graph, the Full code will have code to generate the graph.

☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 174 views
  • 3 likes
  • 4 in conversation