BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sigma_exp
Obsidian | Level 7

So I am running into some issues here: 

Following is the macro I am running to estimate the rate per 1000. The PROC GENMOD is running without any issue, however, in the second part where I am trying to access the same variable and perform some arithmetic operation, it is throwing the following error: 

" Statement is not valid or it is used out of proper order"

 

Wondering it has to do with accessing the macro variable and corresponding numeric value from the dataset internally. But not quite getting the coding right. Any feedback or suggestion is appreciated! 

 

Thanks!

 

%macro rate1(dat,vars);
%do i = 1 %to %sysfunc(countw(&vars.));
	%put %scan(&vars., &i.);
	%LET newv=%scan(&vars., &i.);

	data new_Results; 
	set &dat.;
	proc genmod data=&dat.;
	model  %scan(&vars., &i.) = / offset=ln dist=poisson lrci;
	estimate 'Mean' intercept 1;
	title " cases for in &newv";
	run;



	proc print data=new_Results;
	id n %scan(&vars., &i.);
	inputval=INPUT(SYMGET(%scan(&vars., &i.));
	IR=inputval/n;
	IR_1K=IR*1000;
	LC=quantile('chisq',0.025, inputval*2)/(n*2);
	UC=quantile('chisq',0.975, (inputval+1)*2)/(n*2);
	res_l=LC*1000;
	res_u=UC*1000;
	title "rate per 1K  another way";
	run;

%END;

%mend rate1;


%run_pr(x_trans,c_tot);
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

So what is the purpose of the macro?

 

It seems to be running GENMOD. 

And then also doing some calculations on and printing the results of those calculations.

 

It looks like you have included the ability to pass in multiple variable names and repeat the process for each.  In that case you might want to limit what variables you print in that last PROC PRINT (or what variables are created in the data step before it).  Otherwise each of those PROC PRINTS will include ALL of the variables in the original dataset, and not just the ones related to the current variable being analyzed.

 

You also might want to learn how to use %LOCAL macro variables.  It can make the coding a little easier to type, understand and maintain.

 

%macro rate1(dat,vars);
%local i var ;
%do i = 1 %to %sysfunc(countw(&vars.));
  %LET var=%scan(&vars., &i.);


title "GENMOD for in &var";
proc genmod data=&dat.;
  model  &var = / offset=ln dist=poisson lrci;
  estimate 'Mean' intercept 1;
run;

data new_Results2;
  set &dat.;
  IR=&var/n;
  IR_1K=IR*1000;
  LC=quantile('chisq',0.025, &var*2)/(n*2);
  UC=quantile('chisq',0.975, (&var+1)*2)/(n*2);
  res_l=LC*1000;
  res_u=UC*1000;
run;

title "rate per 1K  for &VAR another way";
proc print data=new_Results2;
  id n &var;
  var IR IR_1K LC UC res_l rec_u;
run;

title;
%END;

%mend rate1;

 

View solution in original post

27 REPLIES 27
tomrvincent
Rhodochrosite | Level 12

You might want to show us the code for the run_pr macro.

sigma_exp
Obsidian | Level 7
872  data x_trans;
873   infile cards;
874   input n c_tot c_etopfa c_sb c_lb;
875   ln=log(n);
876   dummy=1;
877   datalines;

NOTE: The data set WORK.X_TRANS has 1 observations and 7 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.00 seconds


879  ;
880  run;

881  %macro rate1(dat,vars);
882  %do i = 1 %to %sysfunc(countw(&vars.));
883      %put %scan(&vars., &i.);
884      %LET newv=%scan(&vars., &i.);
885
886      data new_Results;
887      set &dat.;
888      proc genmod data=&dat.;
889      model  %scan(&vars., &i.) = / offset=ln dist=poisson lrci;
890      estimate 'Mean' intercept 1;
891      title " cases for in &newv";
892      run;
893
894
895
896      proc print data=new_Results;
897      id n %scan(&vars., &i.);
898      inputval=INPUT(SYMGET(%scan(&vars., &i.));
899      IR=inputval/n;
900      IR_1K=IR*1000;
901      LC=quantile('chisq',0.025, inputval*2)/(n*2);
902      UC=quantile('chisq',0.975, (inputval+1)*2)/(n*2);
903      res_l=LC*1000;
904      res_u=UC*1000;
905      title "rate per 1K  another way";
906      run;
907
908  %END;
909
910  %mend rate1;

911  %run_pr(x_trans,c_tot);
c_tot

NOTE: There were 1 observations read from the data set WORK.X_TRANS.
NOTE: The data set WORK.NEW_RESULTS has 1 observations and 7 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.00 seconds


NOTE: Writing HTML Body file: sashtml4.htm


NOTE: Fitting saturated model. Scale will not be estimated.
NOTE: Algorithm converged.
NOTE: The scale parameter was held fixed.
NOTE: PROCEDURE GENMOD used (Total process time):
      real time           0.84 seconds
      cpu time            0.26 seconds


ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric
       operand is required. The condition was: NUVAR
ERROR: The macro RUN_PR will stop executing.

 

This is the code I am getting

Tom
Super User Tom
Super User

Looks like the macro run_pr has a bug.

Do you have the source code that macro?

 

sigma_exp
Obsidian | Level 7

This is the code I wrote.  

%macro rate1(dat,vars);
%do i = 1 %to %sysfunc(countw(&vars.));
	%put %scan(&vars., &i.);
	%LET newv=%scan(&vars., &i.);

	data new_Results; 
	set &dat.;
	proc genmod data=&dat.;
	model  %scan(&vars., &i.) = / offset=ln dist=poisson lrci;
	estimate 'Mean' intercept 1;
	title " cases for in &newv";
	run;



	proc print data=new_Results;
	id n %scan(&vars., &i.);
	inputval=INPUT(SYMGET(%scan(&vars., &i.));
	IR=inputval/n;
	IR_1K=IR*1000;
	LC=quantile('chisq',0.025, inputval*2)/(n*2);
	UC=quantile('chisq',0.975, (inputval+1)*2)/(n*2);
	res_l=LC*1000;
	res_u=UC*1000;
	title "rate per 1K  another way";
	run;

%END;

%mend rate1;


%run_pr(x_trans,c_tot);
ballardw
Super User

Unless proc print has changed a lot since version 9.4M4 none of the highlighted code is valid in Proc Print:

proc print data=new_Results;
897      id n %scan(&vars., &i.);
898      inputval=INPUT(SYMGET(%scan(&vars., &i.));
899      IR=inputval/n;
900      IR_1K=IR*1000;
901      LC=quantile('chisq',0.025, inputval*2)/(n*2);
902      UC=quantile('chisq',0.975, (inputval+1)*2)/(n*2);
903      res_l=LC*1000;
904      res_u=UC*1000;
905      title "rate per 1K  another way";
906      run;

Print does just that, Prints. It can do some column sums but those other calculations are right out.

Also the INPUT function would require a format and I strongly suspect you do not actually have the macro variable you would be attempting to reference with that SYMGET call.

sigma_exp
Obsidian | Level 7
When I run as a DATA step without using macro, it gives out the specific estimates. For example, instead of using "inputval", if I directly use the "c_tot") with the data=new_Results, it has those estimates. I wonder how can I access the value of "c_tot" by referencing it as a macro variable and perform the arithmetic operations? I guess this is a better way to rephrase my original question.
Tom
Super User Tom
Super User

Why did you send the code for the macro RATE1 when you are not calling that macro?

You are calling the macro RUN_PR.

Reeza
Super User

@sigma_exp wrote:

This is the code I wrote.  

%macro rate1(dat,vars);
%do i = 1 %to %sysfunc(countw(&vars.));
	%put %scan(&vars., &i.);
	%LET newv=%scan(&vars., &i.);

	data new_Results; 
	set &dat.;
	proc genmod data=&dat.;
	model  %scan(&vars., &i.) = / offset=ln dist=poisson lrci;
	estimate 'Mean' intercept 1;
	title " cases for in &newv";
	run;



	proc print data=new_Results;
	id n %scan(&vars., &i.);
	inputval=INPUT(SYMGET(%scan(&vars., &i.));
	IR=inputval/n;
	IR_1K=IR*1000;
	LC=quantile('chisq',0.025, inputval*2)/(n*2);
	UC=quantile('chisq',0.975, (inputval+1)*2)/(n*2);
	res_l=LC*1000;
	res_u=UC*1000;
	title "rate per 1K  another way";
	run;

%END;

%mend rate1;


%run_pr(x_trans,c_tot);

 

Why do you create newv macro variable but never use it?

 

tomrvincent
Rhodochrosite | Level 12
that's the code for rate1. We need to see the code for run_pr. Get it?
Reeza
Super User
NOTE: The data set WORK.NEW_RESULTS has 1 observations and 7 variables.

If your subset has 1 observation then you cannot run a regression. Something is wrong with your input data.

 

 

sigma_exp
Obsidian | Level 7

As a count data, I have the parameter estimates and without running the MACRO, in a DATA with PROC GENMOD, it is running without giving any error. 

Reeza
Super User
You're right, technically no errors.

NOTE: Fitting saturated model. Scale will not be estimated.
NOTE: Algorithm converged.
NOTE: The scale parameter was held fixed.
tomrvincent
Rhodochrosite | Level 12
still not seeing the code for run_pr...can't help you without it.
sigma_exp
Obsidian | Level 7

My mistake: please see the code below:

 



%macro rate1(dat,vars); %do i = 1 %to %sysfunc(countw(&vars.)); %put %scan(&vars., &i.); %LET newv=%scan(&vars., &i.); data new_Results; set &dat.; proc genmod data=&dat.; model %scan(&vars., &i.) = / offset=ln dist=poisson lrci; estimate 'Mean' intercept 1; title " cases for in &newv"; run; proc print data=new_Results; id n %scan(&vars., &i.); inputval=INPUT(SYMGET(%scan(&vars., &i.)); IR=inputval/n; IR_1K=IR*1000; LC=quantile('chisq',0.025, inputval*2)/(n*2); UC=quantile('chisq',0.975, (inputval+1)*2)/(n*2); res_l=LC*1000; res_u=UC*1000; title "rate per 1K another way"; run; %END; %mend rate1; %rate1(x_trans,c_tot);

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 27 replies
  • 1535 views
  • 13 likes
  • 5 in conversation