BookmarkSubscribeRSS Feed
juanvg1972
Pyrite | Level 9

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;

6 REPLIES 6
LinusH
Tourmaline | Level 20

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
Reeza
Super User

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;

Tom
Super User Tom
Super User

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;

Ksharp
Super User

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

Ksharp
Super User

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

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1685 views
  • 2 likes
  • 5 in conversation