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

Hello everyone,

 

What is wrong here?

 

%macro HELLO;
%global game;
%let game = 0;
run;
%mend HELLO;
%HELLO;

data out.TV (KEEP=STUDYID DOMAIN VISITNUM VISIT VISITDY ARMCD ARM TVSTRL TVENRL);
	retain STUDYID DOMAIN VISITNUM; 							
	set TV;

		if &game. ne 0 then abort cancel;
		if &game. ne 0 then 
		
		putlog'WARNING: A prior error has occurred or environment is not correctly initialised. WARNING: SDTME_TV_ALL_V01 terminated';

		if &game. eq 0 then
%do;
/*And then the rest of the program follows*/

The task is to terminate the program without output if the global macro variable is not 0, and then write the warning in the log.

 

I hope you can help me. Iv'e never worked with global macro variable before, or tried to write something in the log.

 

Best regards,

1 ACCEPTED SOLUTION

Accepted Solutions
jimbarbour
Meteorite | Level 14

Hello, @Ninna1,

 

@Kurt_Bremser is quite correct.  You cannot have a %do inside of a Data step as part of the Data step logic.  I would change your code as shown below. 

 

Comments:  

  1. Take the RUN out of your Hello macro.  It doesn't belong there.
  2. Create a little macro to abort the program if needed.  In my environment, I put this macro in an Autocall library so that all my programs can use it.
  3. Use a macro IF statement to check the macro variable that holds your code (&Game).  It's not necessary to put a Macro IF statement inside a %Macro - %Mend structure as of SAS 9.4 M5 (but you must use a %Do - %End, and you cannot nest %IF statements).  The macro %IF statement issues the warning and aborts the program before any subsequent code even starts.  The data set Out.TV cannot be affected, and your Data step doesn't have a lot of extraneous code in it.
  4. The %BQUOTE protects you if a non-numeric value (or no value at all) is in the macro variable &Game.

 

%macro HELLO;
    %global game;
    %let game = 0;
%mend HELLO;
%HELLO;

%MACRO  Abort_Pgm;
    %ABORT CANCEL;
%MEND;

%IF  %BQUOTE(&Game) ^= 0  THEN
    %DO;
        %PUT  WARNING: A prior error has occurred or environment is not correctly initialized. ;
        %PUT  WARNING: SDTME_TV_ALL_V01 terminated';
        %Abort_Pgm;
    %END;

data out.TV (KEEP=STUDYID DOMAIN VISITNUM VISIT VISITDY ARMCD ARM TVSTRL TVENRL);
	retain STUDYID DOMAIN VISITNUM; 							
	set TV;
/* the rest of the program follows*/

Regards,

Jim

View solution in original post

4 REPLIES 4
jimbarbour
Meteorite | Level 14

Hello, @Ninna1,

 

@Kurt_Bremser is quite correct.  You cannot have a %do inside of a Data step as part of the Data step logic.  I would change your code as shown below. 

 

Comments:  

  1. Take the RUN out of your Hello macro.  It doesn't belong there.
  2. Create a little macro to abort the program if needed.  In my environment, I put this macro in an Autocall library so that all my programs can use it.
  3. Use a macro IF statement to check the macro variable that holds your code (&Game).  It's not necessary to put a Macro IF statement inside a %Macro - %Mend structure as of SAS 9.4 M5 (but you must use a %Do - %End, and you cannot nest %IF statements).  The macro %IF statement issues the warning and aborts the program before any subsequent code even starts.  The data set Out.TV cannot be affected, and your Data step doesn't have a lot of extraneous code in it.
  4. The %BQUOTE protects you if a non-numeric value (or no value at all) is in the macro variable &Game.

 

%macro HELLO;
    %global game;
    %let game = 0;
%mend HELLO;
%HELLO;

%MACRO  Abort_Pgm;
    %ABORT CANCEL;
%MEND;

%IF  %BQUOTE(&Game) ^= 0  THEN
    %DO;
        %PUT  WARNING: A prior error has occurred or environment is not correctly initialized. ;
        %PUT  WARNING: SDTME_TV_ALL_V01 terminated';
        %Abort_Pgm;
    %END;

data out.TV (KEEP=STUDYID DOMAIN VISITNUM VISIT VISITDY ARMCD ARM TVSTRL TVENRL);
	retain STUDYID DOMAIN VISITNUM; 							
	set TV;
/* the rest of the program follows*/

Regards,

Jim

jimbarbour
Meteorite | Level 14

You are welcome.  Good luck with your program.  🙂

 

Jim

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 4 replies
  • 601 views
  • 2 likes
  • 3 in conversation