BookmarkSubscribeRSS Feed
Srigyan
Quartz | Level 8

I have two codes

 

Code-1

 

%macro Zero (Input=, Output=);
data &Output.;
   set &Input.;
   array change _numeric_;
        do over change;
            if change=. then change=0;
        end;
 run ;
 %mend;

Code-2

 

%macro Zero (Input=, Output=);
data &Output.;
   set &Input.;
   array change _numeric_;
        %do %over change;
            %if change=. %then change=0;
        %end;
 run ;
 %mend;

 the only difference is % sign with loop in code-2. I get the same output.

Question is:

1) Why and where % sign is require for macro.

2) if the output is same in both the query why do we need % sign for loop here

5 REPLIES 5
Tom
Super User Tom
Super User

 

Your first macro definition looks normal.  You are using the macro to make the names of the input and output datasets dynamic.

Your second macro definition makes no sense and will not work.

  • What code are you trying to conditionally generate by using %DO and %IF?
  • Did you define a macro named OVER somewhere?  There isn't any macro statement named %OVER.

  • The five character string change will never be equal to period so the code CHANGE=0 will never be generated.  Plus if you wanted the macro to generate that as a statement you will need another semi-colon.  The one after the zero is currently going to be interpreted as ending the %IF/%THEN statement.

 

Kurt_Bremser
Super User

Arrays are DATA STEP ELEMENTS and can therefore NOT BE USED AS MACRO ELEMENTS!

It is the same for all data step variables.

The macro preprocessor is not designed to handle data, it is designed to handle code.

ballardw
Super User

@Srigyan wrote:

I have two codes

 

Code-1

 

%macro Zero (Input=, Output=);
data &Output.;
   set &Input.;
   array change _numeric_;
        do over change;
            if change=. then change=0;
        end;
 run ;
 %mend;

Code-2

 

%macro Zero (Input=, Output=);
data &Output.;
   set &Input.;
   array change _numeric_;
        %do %over change;
            %if change=. %then change=0;
        %end;
 run ;
 %mend;

 the only difference is % sign with loop in code-2. I get the same output.

Question is:

1) Why and where % sign is require for macro.

2) if the output is same in both the query why do we need % sign for loop here


Did you actually read the log?

48   %macro Zero (Input=, Output=);
49   data &Output.;
50      set &Input.;
51      array change _numeric_;
52           %do %over change;
ERROR: An unexpected semicolon occurred in the %DO statement.
ERROR: A dummy macro will be compiled.
53               %if change=. %then change=0;
54           %end;
55    run ;
56    %mend;
NOTE: The macro ZERO completed compilation with errors.
      0 instructions 0 bytes.


And executing the macro:

57   %zero(input=junk, output=work.j)
     -
     180
WARNING: Apparent invocation of macro ZERO not resolved.

 

I suspect that you also reused the same data set names and because the macro with the second definition did not execute that the original output data set was not replaced. So you only think it generated the same output. 

 

Moral of the story:

1) Read the log

2) Different code should use different macro names so you know which you are actually testing

3) Generate different output data set names to actually compare the results.

 

Ksharp
Super User
data class;
 set sashelp.class;
 if _n_=1 then call missing(weight,height);
run;

proc stdize data=class out=want missing=0 reponly;
run;

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
  • 5 replies
  • 631 views
  • 0 likes
  • 5 in conversation