DATA Step, Macro, Functions and more

Issue using SYMGET inside a macro

Reply
Contributor
Posts: 36

Issue using SYMGET inside a macro

Hello,
I call a first macro to calculate &type.

Then I would like to update a field of a table from the macro; I try to use SYMGET to do it.

data out;
set In;
%TestsFormat(code, 'NUM', '1','3', code_);
run;


%Macro TestsFormat(MyVarIn, MyType, MyValueOk, MyValueNok, MyVarOut) / store;
%verif_num_char_date(&MyVarIn);
%If &type ne &MyType %Then &MyVarOut=symget("MyValueNok"); %Else &MyVarOut=symget("MyValueOk");
%Mend;

%macro verif_num_char_date(MyVar) / store;
%global type;
DATA _NULL_;
var=input("&MyVar" ,?? COMMA8.);
IF var ne . then call symput('type','NUM');
ELSE do;
var=input("&MyVar",ANYDTDTM.);
if var ne . then call symput('type','DAT');
else call symput('type','CHAR');
end;
run;
%mend;

I have the following Error message :

NOTE: Generated line by the macro variable "MYVAROUT".
1 code_
-----
180

ERROR 180-322: Incorrect Instruction

If someone has an idea, it would be great.

Thanks. Message was edited by: phil27
Contributor
Posts: 48

Re: Issue using SYMGET inside a macro

I think the problem is here:
your macro creates something like this statemets:

data out;
set In;

data _null_;
/* your code */
run;
code_ =
run;

you can use options mprint; to see what your macro creates
Super Contributor
Super Contributor
Posts: 3,174

Re: Issue using SYMGET inside a macro

It's best to start with a more basic DATA step execution to exercise your DATA step (and / or PROC steps too) logic. Then look at how you can inject macro language constructs for more flexibility.

Firstly, I see a DATA _NULL_ statement followed directly by an assignment that uses the INPUT function, however there would be no variable value assigned. I suspect that you thought that you could use a mix of SYMPUT and SYMGET within the same DATA step, then inserted the DATA _NULL_ when it didn't work for you.

Basically there are fundamental issues with your technique - one that attempts to mix macro language (a compilation time event) and DATA step processing.

Scott Barry
SBBWorks, Inc.


SAS Macro Language: Reference, DATA Step Functions for Macros
http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/a003167025.htm

Intermediate and Advanced SAS® Macros
Steven First, Katie Ronk, Systems Seminar Consultants, Madison, WI
http://www2.sas.com/proceedings/sugi31/107-31.pdf
PROC Star
Posts: 1,760

Re: Issue using SYMGET inside a macro

As said above, many problems with your code.
You need to get up to speed with the macro language, and might want to consider proper training.
In this case, think of the macro language as a code generator.

This will work:

[pre]
%Macro TestsFormat(MyVarIn, MyType, MyValueOk, MyValueNok, MyVarOut);
VAR=input(&MyVarIn ,?? COMMA8.);
if VAR ne . then TYPE='NUM';
else do;
VAR=input(&MyVarIn,?? ANYDTDTM.);
TYPE=ifc( VAR ne . , 'DAT', 'CHAR');
end;
&MyVarOut=ifc(TYPE = &MyType, &MyValueOk, &MyValueNok);
%Mend;
[/pre][pre]

data OUT;
set SASHELP.CLASS;
AGEC=put(age,3.);
%TestsFormat(AGEC, 'NUM', '1','3', code_);
run;

[/pre]

Note that a date like 20091231 will end up as NUM here.
Ask a Question
Discussion stats
  • 3 replies
  • 166 views
  • 0 likes
  • 4 in conversation