I am having trouble with the following %macro for a regression.
Basically, I want it to run whenever i=j, and i=1 to 12 and j=1 to 12.
%macro reg(num=);
%do i=1 %to #
%do j=1 %to #
proc reg data=ccy.eur; *Specify Currency Data Set Here;
model Ft_leadSt&i = Ft_St&j;
where &i=&j;
ods output parameterestimates (persist) =dpara1;
run;
%end;
%end;
%mend;
%reg(num=12)
The problem is my output seems to stop when i=9 and j=9. I haven't been able to figure out why?
Screen Shot of data
I would rewrite this without the double loop and only 1 PROC REG statement:
%macro reg(num=);
proc reg data=ccy.eur; *Specify Currency Data Set Here;
ods output parameterestimates (persist) =dpara1;
var FT_leads1 - FT_leads&n FT_St1 - FT_St&n;
%do i=1 %to #
_&i: model Ft_leadSt&i = Ft_St&i;
run;
%end;
quit;
%mend;
%reg(num=12)
Make sure you have those variables in the data set.
Related
I am looking to have a programme which cleans up some messy data I have, I am looking to do this for both the assets and liabilities side of the project i'm working on.
My question is there a way to use a do loop to use the cleaning up data to first clean up the assets then liabilities. something like this:
%do %I = Asset %to Liability;
%assetorliability= I ;
proc sort data = &assetorliability;
by price;
run;
data want&assetorliability;
set &assetorliability;
if _N_ < 50000;
run;
the actual script is quite long so a singular macro may not be the ideal solution but this loop would be great.
TIA.
EDIT : the programme includes some macros and the errors received are as follows:
%let list =Asset Liability;
%do i=1 %to %sysfunc(countw(&list,%str( )));
%let next=%scan(&list,&i,%str( ));
%Balance;
%end;
in the macro the data steps are named with a balance&list to allow for each scenario. the errors are:
13221 %let list =Asset Liability;
13222 %do i=1 %to %sysfunc(countw(&list,%str( )));
ERROR: The %DO statement is not valid in open code.
13223
13224 %let next=%scan(&list,&i,%str( ));
WARNING: Apparent symbolic reference I not resolved.
WARNING: Apparent symbolic reference I not resolved.
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric
operand is required. The condition was: &i
ERROR: Argument 2 to macro function %SCAN is not a number.
ERROR: The %END statement is not valid in open code.
The macro %do statement is not as flexible as the data step do statement. To loop over a list of values you would want to put the list into a macro variable and use an index variable in your %do loop.
Note that macro logic needs to be inside of a macro. You cannot use it in "open" code.
%macro do_over(list);
%local i next;
%do i=1 %to %sysfunc(countw(&list,%str( )));
%let next=%scan(&list,&i,%str( ));
proc sort data = &next ;
by price;
run;
data want&next ;
...
%end;
%mend do_over ;
%do_over(Asset Liability)
I started to learn %macro in SAS and now I'm trying to implement simple bootstrap with histogram as an output.
/*Create K data sets(vectors)*/
%macro datasets(K);
%do i=1 %to &K;
data indata&i;
%do j = 1 %to 50;
x=(rand('normal',2,9));
output;
%end;
run;
%end;
%mend datasets;
%datasets(3);
/*Bootstrap and hist*/
%macro boot (data,res);
%do i=1 %to &res;
%let x = (sample(&data,50));
%let m = (mean(&x));
%end;
proc iml;
read &m into A;
create DataM from A;
append from A;
close Data1;
quit;
proc univariate data=Data1;
histogram m;
run;
%mend boot;
%boot(Indata1,100);
It doesn't work and I can't understand why. Can you point me the mistake?
Use PROC SURVEYSELECT to generate bootstrap samples then do bootstrap analysis by Replication (a variable created by SURVEYSELECT). Your macro idea will be far too slow.
As mentioned use Proc SurveySelect and Proc Means. You can select all 100 samples in one Proc SurveySelect and then apply Proc Means with a BY statement to calculate the means in one step. Macro's don't add anything to the solution here.
I'm posting both solutions - the macro solution does take longer as well.
*Without macro;
proc surveyselect data=indata1 out=rsample method=srs n=50 reps=100;
run;
proc means data=rsample noprint;
by replicate;
var x;
output out=Data1 mean(x)=m;
run;
proc univariate data=Data1;
histogram m;
run;
*Macro solution;
%macro boot(data, res);
%do i=1 %to &res;
%*Currently pulls the same sample every time but you can fix that part;
proc surveyselect data=&data out=x method=srs n=50 reps=1 seed=343434;
run;
proc means data=x noprint;
var x;
output out=m mean(x)=m;
run;
proc append base=DataM data=m;
run;
%end;
%mend;
%boot(Indata1,10);
Perhaps it will help if we outline some of the ways that the posted macro code does NOT work. If nothing else then as examples of things to avoid.
If the first macro , %datasets(), you are using a macro %DO loop where you should use a normal data step DO loop. Also make sure to define your local macro variables as local. This will prevent the macro from modifying the value of an existing macro variable with the same name.
/*Create K data sets(vectors)*/
%macro datasets(K);
%local i ;
%do i=1 %to &K;
data indata&i;
do j = 1 to 50;
x=(rand('normal',2,9));
output;
end;
drop j;
run;
%end;
%mend datasets;
In the second macro you have a %DO loop that does nothing.
%do i=1 %to &res;
%let x = (sample(&data,50));
%let m = (mean(&x));
%end;
You repeat the exact same %LET statements multiple times. The result does not change since the loop variable i is not referenced at all. If you called the macro with data=indata1 then the result of the two statements will be that X=(sample(indata1,50)) and that M=(mean((sample(indata1,50)))). I think that perhaps you intended that the strings sample and mean might take some action, but since they have no macro triggers (& or %) they are just streams of characters to the macro processor.
I am not an expert on IML, but those statements also do not look like they are doing much.
I have a problem with SAS. More precisely with calling macro, which is inside another macro. Here is the example.
data TEST_1;
do i = 1 to 100;
a=i**2;
output;
end;
run;
data TEST_2;
do i = 1 to 100;
b=i**3;
output;
end;
run;
%macro macro_in(file_a);
data result1;
set &file_a;
c=a+1;
run;
%mend;
%macro_in(TEST_1);
%macro macro_out(file_b);
data result2;
set &file_b._2;
d=a-1;
run;
data _null_;
do i = 1 to 2;
call execute(COMPRESS('%macro_in(' || &file_b || '_' || i || ')'));
output;
end;
run;
%mend;
%macro_out(TEST);
First macro works completely fine, however there is a slight problem with variable file_b i the second macro (Code cannot use it as an argument to the inner macro). Thanks for any help!
When calling macro_in from macro_out you do not need a datastep, you can use the macro language:
%macro macro_out(file_b);
data result2;
set &file_b._2;
d=a-1;
run;
%do i = 1 %to 2;
%macro_in(&file_b._&i);
%end;
%mend;
In SAS I can use this handy snippet to do something like this.
%let listofvars = work.apples work.bananas work.oranges;
%let var_no = 1;
%let var = %scan(&listofvars, &var_no, ' ');
%do %while (&var ne);
proc sort data = &var;
by id;
run;
%let var_no = %eval(&var_no +1);
%let var = %scan(&listofvars, &var_no, ' ');
%end;
To sort each of those datasets.
I'd quite like to reduce the snippet to a loop macro, so I can do something like this:
%let setlist = work.apples work.bananas work.oranges;
%macro mymacro(dataset);
proc sort data = &dataset.
by id;
run;
%mend;
%loop(&setlist, mymacro);
/*the loop macro will know to pass the &var. in as a arguement to the macro*/
This will make for much better code readability.
Is this possible?
Yes. The name macro routine can be a macro. Macros "write" SAS code for you.
%macro create(dataset);
data &dataset;
do i=1 to 10;
id=rannor(0);
output;
end;
run;
%mend;
%macro sort(dataset);
proc sort data=&dataset;
by id;
run;
%mend;
%macro loop(list,mcr);
%local i n val ;
%let n=%sysfunc(countw(&list));
%do i=1 %to &n;
%let val = %scan(&list,&i);
%&mcr(&val);
%end;
%mend;
%let sets = apples oranges pears;
options mprint;
%loop(&sets,create);
%loop(&sets,sort);
I have a macro variable like &a having value of (1234.45)*. I am trying to replace the ( and ) from the macro and replace them with a negative mark since its a negative number.
%let a=(1234.45)
Some of the options which I have appplied are
%macro test1;
%if %substr(&a,1,1) = '(' %then %do;
%let b=%substr(&a,1,'-')
%end;
%mend;
%test1
This is numeric conversion and best handled in a data step. If for some reason you really need a macro variable, use SYMPUT.
%let a=(1234.45);
data _null_;
x=input("&a.",comma10.);
call symputx("b",x);
run;
%put &=a &=b;