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;
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.
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;
Thanks for your help!!
Juan V.
Utilidades, recursos, ejemplos y documentación sobre el software de análisis de datos SAS
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;
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
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
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!
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.