SAS macro does not run or run without stopping

Reply
Occasional Contributor
Posts: 10

SAS macro does not run or run without stopping

[ Edited ]

I am having an issue with macro function. The SAS code works just fine when not used in the macro function.  Attached is the code for the first macro and the second macros is almost the same but with some more programming on a third variable the first macro does not have . 

 

When I run the first macro, it ran most of the time but sometime it does run and the log is just black line of codes.

When I run the second macro (not attached here), it did not run for most of the time, and it works mostly only if I close sas program and rerun the second macro without running the first one. And a lot of time it runs without stopping, which drives me crazy.

 

I would appreciate any help from anyone who had encountered the same problem like me. Thanks a lot. 

%macro Exam(Program);
.....................
%mend Exam;

%Exam(Program=XY);
%Exam(Program=UW);
%Exam(Program=ST);





%Macro Exam2(Program, Class);
.....................
%mend Exam2;
%Exam2(Program=ABC, Class=D);

 



 

Super User
Posts: 8,220

Re: SAS macro does not run or run without stopping

Posted in reply to AngelaFan

Given the size of your code, I don't have time to review it carefully. However, a couple of points that you might want to address:

 

1. You use * comments. DON'T! They often will end up being part of the code, rather than the comment you were trying to insert. Instead use either %* comment OR /* comment */

2. Your macro appears to rely on some macro variables that are created outside of the macro. I try to include all such variables as parameters to the macro

3. When testing your macro, if often helps to set: options mprint mlogic symbolgen; Those option settings provide a lot of detail one needs to debug a macro

 

Art, CEO, AnalystFinder.com

 

Occasional Contributor
Posts: 10

Re: SAS macro does not run or run without stopping

Thank you Sir. I can work on the comment part, but all the macro variables are specified in the very beginning so that I can use it throughout my code whenever I refer to a macro variable. I also appreciate the debug suggestion. I will give it a try. Thanks, 

Super User
Posts: 2,516

Re: SAS macro does not run or run without stopping

Posted in reply to AngelaFan

I specifically used %mend with the macro name to tell sas please stop the corresponding macro. 

The word after mend is not useful to SAS, consider it a comment for yourself.

 

You may not like it, but the symptoms you indicate point to something not being closed properly, as you say.

 

Maybe it's due to some macro variables containing unbalanced quotes, or something similar. Who knows. You have to study the log now.

 

 

 

 

Occasional Contributor
Posts: 10

Re: SAS macro does not run or run without stopping

I do not agree with you on that. lol~~~And I check log for every line of code. Thanks for the response. 

Super User
Posts: 13,947

Re: SAS macro does not run or run without stopping

Posted in reply to AngelaFan

Is the purpose of this code to read the second line of a data file for variable names so you can use Proc Import to read data?

 

If so try something like

data _null_;
   infile "&win5.\&Program._Input.txt";
   file "&win5.\&Program._Input2.txt";
   input;
   If _n_>1 then put _infile_;
run;

and use Proc import to read the _input2.txt version of the file.

 

 

If these files occur in the same layout(s) you would be better off writing a proper data step to read each layout where you control the variable names, format and informats. If you know what order the variables are then using the option firstobs=3 would read the first line of data.

Occasional Contributor
Posts: 10

Re: SAS macro does not run or run without stopping

When the macro function runs, the data are read in properly. The second line is just all the variable names I want to extract and use later. Thank you for the response. 

Respected Advisor
Posts: 4,802

Re: SAS macro does not run or run without stopping

Posted in reply to AngelaFan

@AngelaFan

The macro you've posted doesn't use any SAS macro logic but it's just a way to call the same code multiple times passing while passing in a different parameter value. That will make it really simple to debug your code.

 

1. Put lines with %macro, %mend and %exam into comment

2. Add a %let Program=XY; at the top of your code

3. Now step through your program, issue one run group after the other from top to bottom and always check if the step returns the expected result.

 

It's much easier to debug SAS data step and Proc code than macro code. So get first this bit right before you wrap a macro around.

 

 

Just some things I've seen in your code:

 

1. I guess you want to use the automatic variable _n_ and not some variable _n

    newname = trim((name))|| "_n";

2. An IN operator for a character variable requires a comma separated list of values in quotes. That's not what your Select Into will populate into &F1count.

  proc sql noprint;
    select AppID
      into:F1count separated by " "  
    from F1examinees;
  quit;

  ***This is F1 data;
  data F1;
    set Pfile2;
    where NAME in (&F1count.);
  run;

 

Occasional Contributor
Posts: 10

Re: SAS macro does not run or run without stopping

I made sure the macro function run with substituting variables. When I put it into macro and ran a second macro with almost the same code, it did not run or run sometimes. Very weird. Thank you for your response 

Super User
Super User
Posts: 8,289

Re: SAS macro does not run or run without stopping

[ Edited ]

@Patrick wrote:

2. An IN operator for a character variable requires a comma separated list of values in quotes. That's not what your Select Into will populate into &F1count.

  proc sql noprint;
    select AppID
      into:F1count separated by " "  
    from F1examinees;
  quit;

  ***This is F1 data;
  data F1;
    set Pfile2;
    where NAME in (&F1count.);
  run;

 


 If the variable NAME used in the WHERE statement is really character then you will need to quote the elements in the list.

select quote(trim(AppID))
  into :F1count separated by " "  
  from F1examinees
;

 

BUT the IN operator is SAS does NOT require commas between the elements in the list.  You can use commas if you want to but in general it will just make the macro variable harder to work with than if you used space as the delimiter.

 

Try it yourself.

 

Occasional Contributor
Posts: 10

Re: SAS macro does not run or run without stopping

The base code works. Thank you.
Super User
Posts: 13,947

Re: SAS macro does not run or run without stopping

Posted in reply to AngelaFan

@AngelaFan wrote:
The base code works. Thank you.

What code?

You have removed all of the actual code so now no one can reference it.

Without code how can anyone suggest corrections?

 

 

Occasional Contributor
Posts: 10

Re: SAS macro does not run or run without stopping

Because everyone is focusing on the code that works, so I delete the code.
When I macro the code, it does not work.
Super User
Posts: 6,939

Re: SAS macro does not run or run without stopping

Posted in reply to AngelaFan

First place I would look:  

 

What is in VARNAMES1?  And what gets written as the value of &COLNAMES?

 

If you have an extra variable or two in the incoming data, you can end up with a blank value for COL1.  That can throw off the logic of the RENAME list when creating PFILE.

Occasional Contributor
Posts: 10

Re: SAS macro does not run or run without stopping

Posted in reply to Astounding

Thank you for your responses. The code works just fine. It is the macro which works funky. 

Ask a Question
Discussion stats
  • 20 replies
  • 159 views
  • 1 like
  • 8 in conversation