DATA Step, Macro, Functions and more

macro errors

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 79
Accepted Solution

macro errors

data eq;
input ID y x z w;
cards;
1 1 65 40 0
2 1 34 . 0
3 0 . 23 0
4 1 48 35 0 
;
run;
%macro em (missper, n_imp, my_seed);
proc mi data=eq seed=&&my_seed&i simple nimpute=&n_imp out=EMIMP&missper ;
    em itprint ;  
    var y x z w;
	run;
%mend em;

%macro mi (missper,n_imp,n_seed);
	%em (&missper,&n_imp,&n_seed);
%mend mi;

%macro large(missper,n_imp,n_seed,n_boot);
 data seed_long(drop=i);
    call streaminit(&n_seed);
    do i = 1 to &n_boot;
      x_seed = floor(rand("Uniform")*500000); output;
    end;
 run;  

data _null_;
   set seed_long;
   call symput('my_seed'|| compress(put(_n_,4.)), x_seed);
  run;

   %let i=1;
   %do %until (&i>&n_boot) ;
	  %mi(&missper)
      %let i = %eval(&i+1);
   %end;
%mend large;

%large(20, 5, 3456,3);

 Hello, 

What I am trying to do is using a random fixed seed to create EM output. but I got the following error.

 

NOTE 137-205: Line generated by the invoked macro "EM".
1 proc mi data=eq seed=&&my_seed&i simple nimpute=&n_imp out=EMIMP&missper ; em
---
22
1 ! itprint ; var y x z w; run;
ERROR 22-322: Syntax error, expecting one of the following: an integer constant, PCTMISSING.

 

 

I know %large has no problem. The problem is the writing of EM and MI macro variable. But I don't know what is the right way to do it.

 

Thanks


Accepted Solutions
Solution
‎04-08-2018 12:20 AM
Super User
Super User
Posts: 8,289

Re: macro errors

Posted in reply to xiangpang

That is easy to debug. You have inserted %PUT and it is "eating" the PROC statement.

1615  %macro tem (missper,n_imp,n_seed);
1616  %PUT proc mi data=eq seed=&&my_seed&i nimpute=&n_imp out=EMIMP&missper;
1617      em itprint outem = outem;
1618      var y x z w;
1619      run;
1620  %mend tem;

View solution in original post


All Replies
Respected Advisor
Posts: 3,293

Re: macro errors

Posted in reply to xiangpang

Place a %put statement before the PROC MI statement, so you can check the values of the macro variables at that instant.

--
Paige Miller
Frequent Contributor
Posts: 79

Re: macro errors

Posted in reply to PaigeMiller

nimpute is not created. but how to fix it? Thanks

 


proc mi data=eq seed= 95419 simple nimpute= out=EMIMP20
NOTE: Line generated by the invoked macro "EM".
1 em itprint ;
--
180

ERROR 180-322: Statement is not valid or it is used out of proper order.

 

Respected Advisor
Posts: 4,802

Re: macro errors

Posted in reply to xiangpang

@xiangpang wrote:

nimpute is not created. but how to fix it? Thanks

 


proc mi data=eq seed= 95419 simple nimpute= out=EMIMP20
NOTE: Line generated by the invoked macro "EM".
1 em itprint ;
--
180

ERROR 180-322: Statement is not valid or it is used out of proper order.

 


@xiangpang

One thing to note which @PaigeMiller hinted: Your macro %MI() expects three parameters to be passed in but you're only passing in one when calling it.

    %mi(&missper)

That means your other macro vars will be blank leading to invalid generated statements.

 

 

Use option MPRINT to see what SAS code the macros actually generate.

Frequent Contributor
Posts: 79

Re: macro errors

I restart SAS and found nimpute problem gone. but still have errors.

 

proc mi data=eq seed= 436565 nimpute=5 out=EMIMP20
NOTE: Line generated by the invoked macro "EM".
1 em itprint;
--
180
MPRINT(EM): em itprint;

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the invoked macro "EM".
1 var y x z w;
---
180
MPRINT(EM): var y x z w;

ERROR 180-322: Statement is not valid or it is used out of proper order.

MPRINT(EM): run;
MPRINT(MI): ;

Super User
Super User
Posts: 8,289

Re: macro errors

Posted in reply to xiangpang

You have written macro's that take three parameters.

%macro mi (missper,n_imp,n_seed);
	%em (&missper,&n_imp,&n_seed);
%mend mi;

You are calling it with only one parameter.

	  %mi(&missper)

So that means that it is calling EM like this:

%em (&missper,,);

Which means this line the %EM macro 

proc mi data=eq seed=&&my_seed&i simple nimpute=&n_imp out=EMIMP&missper ;

So if the macro variable I resolves to 1 then you are generating this code 

proc mi data=eq seed=&1 simple nimpute= out=EMIMP&missper ;

Now &1 is not a valid macro variable reference so it will stay as &1 is is clearly not a number that can be used for a seed.

And for the NIMPUTE= option you are not giving any value at all.

Frequent Contributor
Posts: 79

Re: macro errors

Thanks. I update the code. 

data eq;
input ID y x z w;
cards;
1 1 65 40 0
2 1 34 . 0
3 0 . 23 0
4 1 48 35 0 
;
run;

%macro tem (missper,n_imp,n_seed);
%PUT proc mi data=eq seed=&&my_seed&i nimpute=&n_imp out=EMIMP&missper.;
	EM itprint outem = outem;  
    var y x z w;
	run;
%mend tem;


 %macro mi (missper,n_imp,n_seed);
	%tem (&missper,&n_imp,&n_seed);
%mend mi;

%macro large(missper,n_imp,n_seed,n_boot);
 data seed_long(drop=i);
    call streaminit(&n_seed);
    do i = 1 to &n_boot;
      x_seed = floor(rand("Uniform")*500000); output;
    end;
 run;  

data _null_;
   set seed_long;
   call symput('my_seed'|| compress(put(_n_,4.)), x_seed);
  run;

   %let i=1;
   %do %until (&i>&n_boot) ;
	  %mi(&missper,&n_imp,&n_seed);
      %let i = %eval(&i+1);
   %end;
%mend large;

%large(20,5,3456,3);

I changed %mi with three parameters. As you can see. the EM is like this :

 

proc mi data=eq seed= 95419 nimpute=5 out=EMIMP20

 

So the parameter is right now. but it still has other error like this: 
NOTE: Line generated by the invoked macro "TEM".
1 EM itprint outem = outem;
--
180
MPRINT(TEM): EM itprint outem = outem;

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the invoked macro "TEM".
1 var y x z w;
---
180

Super User
Super User
Posts: 8,289

Re: macro errors

Posted in reply to xiangpang

Where is the "1" coming from? It is really hard to figure out error messages without the full log. Also please use the pop-up windows that open when you click on the Insert Code icon on the menu bar instead of pasting into the regular window. This will prevent the forum editor from reflowing the text.

 

Are you sure that your file does not contain some strange character.

 

How are you running SAS? Are using SAS Display Manger? Or SAS/Studio or SAS Enterprise guide or some other "workspace" method of running SAS?

 

Frequent Contributor
Posts: 79

Re: macro errors

1605  data eq;
1606  input ID y x z w;
1607  cards;

NOTE: The data set WORK.EQ has 4 observations and 5 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds


1612  ;
1613  run;
1614
1615  %macro tem (missper,n_imp,n_seed);
1616  %PUT proc mi data=eq seed=&&my_seed&i nimpute=&n_imp out=EMIMP&missper;
1617      em itprint outem = outem;
1618      var y x z w;
1619      run;
1620  %mend tem;
1621
1622   %macro mi (missper,n_imp,n_seed);
1623      %tem (&missper,&n_imp,&n_seed);
1624  %mend mi;
1625
1626  %macro large(missper,n_imp,n_seed,n_boot);
1627   data seed_long(drop=i);
1628      call streaminit(&n_seed);
1629      do i = 1 to &n_boot;
1630        x_seed = floor(rand("Uniform")*500000); output;
1631      end;
1632   run;
1633
1634  data _null_;
1635     set seed_long;
1636     call symput('my_seed'|| compress(put(_n_,4.)), x_seed);
1637    run;
1638
1639     %let i=1;
1640     %do %until (&i>&n_boot) ;
1641        %mi(&missper,&n_imp,&n_seed);
1642        %let i = %eval(&i+1);
1643     %end;
1644  %mend large;
1645
1646  %large(20,5,3456,3);
MPRINT(LARGE):   data seed_long(drop=i);
MPRINT(LARGE):   call streaminit(3456);
MPRINT(LARGE):   do i = 1 to 3;
MPRINT(LARGE):   x_seed = floor(rand("Uniform")*500000);
MPRINT(LARGE):   output;
MPRINT(LARGE):   end;
MPRINT(LARGE):   run;

NOTE: The data set WORK.SEED_LONG has 3 observations and 1 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.00 seconds


MPRINT(LARGE):   data _null_;
MPRINT(LARGE):   set seed_long;
MPRINT(LARGE):   call symput('my_seed'|| compress(put(_n_,4.)), x_seed);
MPRINT(LARGE):   run;
NOTE: Numeric values have been converted to character values at the places given by:
      (Line):(Column).
      5893:233
NOTE: There were 3 observations read from the data set WORK.SEED_LONG.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds


proc mi data=eq seed=       95419 nimpute=5 out=EMIMP20
NOTE: Line generated by the invoked macro "TEM".
1            em itprint outem = outem;
             --
             180
MPRINT(TEM):   em itprint outem = outem;

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the invoked macro "TEM".
1                                          var y x z w;
                                           ---
                                           180
MPRINT(TEM):   var y x z w;

ERROR 180-322: Statement is not valid or it is used out of proper order.

MPRINT(TEM):   run;
MPRINT(MI):  ;
MPRINT(LARGE):  ;
proc mi data=eq seed=      450752 nimpute=5 out=EMIMP20
NOTE: Line generated by the invoked macro "TEM".
1            em itprint outem = outem;
             --
             180
MPRINT(TEM):   em itprint outem = outem;

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the invoked macro "TEM".
1                                          var y x z w;
                                           ---
                                           180
MPRINT(TEM):   var y x z w;

ERROR 180-322: Statement is not valid or it is used out of proper order.

MPRINT(TEM):   run;
MPRINT(MI):  ;
MPRINT(LARGE):  ;
proc mi data=eq seed=      436565 nimpute=5 out=EMIMP20
NOTE: Line generated by the invoked macro "TEM".
1            em itprint outem = outem;
             --
             180
MPRINT(TEM):   em itprint outem = outem;

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the invoked macro "TEM".
1                                          var y x z w;
                                           ---
                                           180
MPRINT(TEM):   var y x z w;

ERROR 180-322: Statement is not valid or it is used out of proper order.

MPRINT(TEM):   run;
MPRINT(MI):  ;
MPRINT(LARGE):  ;

Thanks for your suggestion. I don't know where is the 1 coming from. I am using sas for student. I can't find a strange character now. 

Solution
‎04-08-2018 12:20 AM
Super User
Super User
Posts: 8,289

Re: macro errors

Posted in reply to xiangpang

That is easy to debug. You have inserted %PUT and it is "eating" the PROC statement.

1615  %macro tem (missper,n_imp,n_seed);
1616  %PUT proc mi data=eq seed=&&my_seed&i nimpute=&n_imp out=EMIMP&missper;
1617      em itprint outem = outem;
1618      var y x z w;
1619      run;
1620  %mend tem;
Frequent Contributor
Posts: 79

Re: macro errors

After I remove %put, It works. but I just added %put. where is the problem coming before it? and from the log, how did you find it?

 

Thanks a lot

Super User
Super User
Posts: 8,289

Re: macro errors

Posted in reply to xiangpang

Who knows what your other errors were about, but your original macro was so messed up perhaps you just left SAS in a confused state.

 

The error message said where to look.  Line generated by the invoked macro "TEM".

proc mi data=eq seed=       95419 nimpute=5 out=EMIMP20
NOTE: Line generated by the invoked macro "TEM".
1            em itprint outem = outem;
             --
             180
MPRINT(TEM):   em itprint outem = outem;

Also notice how the PROC line does not have the MPRINT(TEM): in front of it like the EM and VAR lines.

Frequent Contributor
Posts: 79

Re: macro errors

Thanks for your reply. I learned macro by myself. Could you give me some suggestion how to improve this macro or how to improve my general knowledge about macro? 

 

have a good day

PROC Star
Posts: 549

Re: macro errors

Posted in reply to xiangpang

apart from wonderful suggestions, Please try executing your code without macro variables and macro. if it works then you can easily map to macro and make it dynamic. 

Frequent Contributor
Posts: 79

Re: macro errors

Thanks for your suggestion. I tried %tem without macro and no problem. I tried some other macro with the %large %mi without any problem. The only difference is they only have one parameter &missper.

☑ This topic is solved.

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

Discussion stats
  • 14 replies
  • 328 views
  • 0 likes
  • 5 in conversation