DATA Step, Macro, Functions and more

calling a macro from a data sep

Reply
Frequent Contributor
Posts: 93

calling a macro from a data sep

I'm trying to create a field in a datasets based in the value of another field.

I'm usind a macro to make se assignment of the value. Yo can see the process bellow.

The real macro is not that simplem but I'ms using it to make tests.

I expect that result:

campo1 campo2

ab         1

ac         2

but I get:

campo1 campo2

ab         1

ac         2

I'm not sure if I have any error in the method of calling a macro from a data sets

Can anybodt hepl me???, any advice would n greatly apreciatted.

----

data prueba;

length campo1 $10.;

campo1 = 'ab';

output;

campo1 = 'ac';

output;

run;

data prueba1;

set prueba;

campo2 = %valor1(campo1);

run;

%macro valor1(campo);

%if %trim(&campo) = 'ab' %then %do;

    %let salida = 1;

%end;

%else %do;

     %let salida = 2;

%end;

&salida;

%mend;

Super User
Posts: 5,256

Re: calling a macro from a data sep

Why are you mixing data step and macro logic? Can't see any reason for you to use macros from your sample in- and outputs.

Data never sleeps
Super User
Posts: 17,784

Re: calling a macro from a data sep

Don't use a macro, use a format instead.

http://www2.sas.com/proceedings/sugi30/001-30.pdf

proc format;

value $ salida_fmt

     'ab' = 1

     'ac' = 2;

run;

data want;

set have;

campo2=put(campo1, $salida_fmt.);

run;

Frequent Contributor
Posts: 93

Re: calling a macro from a data sep

Super User
Super User
Posts: 6,499

Re: calling a macro from a data sep

Macro code generates SAS code to be included in your program for SAS to execute.  You cannot use it to test values of dataset variables, just the strings that you have passed it in macro variables.

You might get what you want be having the macro generate the data step code that you need.

%macro valor1(source,target);

if &source = 'ab' then &target =  1;

else &target = 2;

%mend;

data prueba1;

set prueba;

%valor1(campo1,campo2);

run;

Super User
Posts: 9,671

Re: calling a macro from a data sep

call execute() is the interface between data step and macro device.

data prueba;
length campo1 $ 10;
campo1 = 'ab';
output;
campo1 = 'ac';
output;
run;

data _null_;
set prueba end=last;
if _n_ eq 1 then call execute('data prueba1;');
call execute('campo1="'||strip(campo1)||'";campo2 = %valor1('||strip(campo1)||');output;');
if last then call execute('run;');
run;
 

%macro valor1(campo);
%if &campo = ab %then %do;
    %let salida = 1;
%end;
%else %do;
     %let salida = 2;
%end;
&salida

%put &salida;
%mend;

Ksharp

Super User
Posts: 9,671

Re: calling a macro from a data sep

If you care about speed ,use resolve() ,resolve() is also the interface between data step and macro device.

data prueba;
length campo1 $ 10;
campo1 = 'ab';
output;
campo1 = 'ac';
output;
run;


%macro valor1(campo);
%if &campo = ab %then %do;
    %let salida = 1;
%end;
%else %do;
     %let salida = 2;
%end;
&salida

%put &salida;
%mend;

data prueba1;
set prueba;
campo2 =resolve( '%valor1('||strip(campo1)||')');
run;
 


 

Ksharp

Ask a Question
Discussion stats
  • 6 replies
  • 304 views
  • 2 likes
  • 5 in conversation