DATA Step, Macro, Functions and more

Create a macro variable based on another dataset

Reply
Contributor
Posts: 32

Create a macro variable based on another dataset

I would like to use estimate value to create a macro variable aa when the wk in data2 matches the wk2 in data1.

The where condition in PROC SQL does not work properly.

Any way to solve this? Thanks.



data data1;

  input wk2 estimate;

datalines;

  1 12

  2 14

  3 15

  ;

run;

%macro trash(var1=, var2=);  

     %global &var2;

     proc sql;

        select distinct estimate into: &var2

        from summ

        where wk2=&var1;

     quit;

%mend trash;

data data2;

  wk=2;

  %trash(var1=wk,var2=aa);

  %put &aa;

run;

proc print data=ori;run;

Super User
Posts: 10,023

Re: Create a macro variable based on another dataset

Posted in reply to sandyming

use Call execute() .

Contributor
Posts: 32

Re: Create a macro variable based on another dataset

works. Any other methods to do this?

Super User
Posts: 5,427

Re: Create a macro variable based on another dataset

Posted in reply to sandyming

From your example, it seems unnecessary to create data2:

data _null_

     set data1;

     where ws = '2';

     call symput('AA',estimate);

run;

Is ws unique in data1?

What do you intend to use &AA for?

Data never sleeps
Contributor
Posts: 60

Re: Create a macro variable based on another dataset

Posted in reply to sandyming

data data1;

  input wk2 estimate;

datalines;

  1 12

  2 14

  3 15

  ;

run;


data j;

     set data1;

     if wk2=2 then call symputx("aa", estimate);

run;

Automatically Macro variables created in Call symputx will be global. if you use this out of %macro %mend;



Regular Contributor
Posts: 195

Re: Create a macro variable based on another dataset

Posted in reply to sandyming

I think it's working by using WHERE clause in proc sql

proc sql noprint;

   select estimate into :aa

   from data1 as a,

          data2 as b,

   where a.wk2 = b.wk;

quit;

%put &aa.;

-Urvish

Valued Guide
Posts: 2,177

Re: Create a macro variable based on another dataset

Posted in reply to sandyming

Do you expect process to execute the way it was laid out with indentation - with the sql macro executing within the data step?

A feature being introduced with SAS9.4 (next month) will allow something like that. However you are not using the relevant syntax.

Check out the function DOSUBL() at support.sas.com

peterC

New Contributor
Posts: 4

Re: Create a macro variable based on another dataset

Posted in reply to sandyming

Hi,

data data1;

  input wk2 estimate;

datalines;

  1 12

  2 14

  3 15

  ;

run;

%macro Test (num=);

proc sql noprint;

select estimate into : aa from data1

where wk2=#

quit;

%put &aa;

data data2;

wk=#

estimate=&aa;

run;

proc print;run;

%mend Test;

/* calling the macros */

%test(num=2);

%test(num=3);

Ask a Question
Discussion stats
  • 7 replies
  • 355 views
  • 3 likes
  • 7 in conversation