DATA Step, Macro, Functions and more

passing a variable to a macro parameter

Reply
Occasional Contributor
Posts: 6

passing a variable to a macro parameter

I haven't worked with macros in awhile, so please bear with me.  I am wanting to simulate some data using specific seed numbers.I have 20 pairs of values for seed1 and seed2. The following is what I have sketched out for one pair of seed1 and seed2 values, but I don't know how to create a loop that runs through the set of 20 values (do while?). appreciate some pointers. thanks jm


%macro xone(seed1= seed2=);

data samples;
do samp = 1 to 100;
do t = 1 to 100;
u = rannor(&seed1)*5;
x = 100 + 3*rannor(&seed2);
y = 10 + 0.5*x + u;
output;
end ;
end;
run;
%mend;

%xone(seed1=1234, seed2=4567);

Super User
Posts: 6,939

Re: passing a variable to a macro parameter

Use call execute:

data _null_;
input seed1 seed2;
call execute('%xone(seed1='!!put(seed1,best.)!!',seed2='!!put(seed2,best.)!!');');
cards;
1234 4567
;
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 6

Re: passing a variable to a macro parameter

Kurt - thank you very much. It worked great.  Each pair of seed1, seed2 has a student's name associated with it.  I want to add in the student's name and have the program print out the seed1, seed2 and name as well as the proc means for the data that are generated for each student. In the code below, I used title &seed1 &seed2 before the proc mean and it worked (altho I am sure there is probably a better way), but when I add in name it doesn't print the name. I made a number of attempts, but am quite unfamiliar with call execute .  any ideas? thanks again!

 

options ls=78 formdlim='*' nodate nonumber nolabel;
ods listing;

%macro xone(seed1,seed2,name);

data samples;
do samp = 1 to 10;
do t = 1 to 100;
u = rannor(&seed1)*5;
x = 100 + 3*rannor(&seed2);
y = 10 + 0.5*x + u;
output;
end ;
end;
run;

title &seed1 &seed2 &name;
proc means;
%mend;

data _null_;
input seed1 seed2 name $;
call execute('%xone(seed1='!!put(seed1,best.)!!',seed2='!!put(seed2,best.)!!',name);');
cards;
1234 4567 Doyle
1117 7111 Boyce

;
run;

Super User
Posts: 6,939

Re: passing a variable to a macro parameter

I think you need a slight change:

call execute('%xone(seed1='!!put(seed1,best.)!!',seed2='!!put(seed2,best.)!!','!!name!!');');
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 6

Re: passing a variable to a macro parameter

Yes, this worked great. thanks again

 

name='!!name!!'

 

Ask a Question
Discussion stats
  • 4 replies
  • 135 views
  • 0 likes
  • 2 in conversation