Using SAS macro - sas

Here is code:
%macro do_regression(dep);
proc glimmix data=xxx;
class group id intv month1;
model &dep = group month1 group*month1/d=normal link=identity;
random intv(id);
lsmeans group*month1/ilink diff cl ;
lsmestimate group*month1 'bsl-3 ' 1 -1 0 0 -1 1 0 0/cl ;
lsmestimate group*month1 'bsl-6' 1 0 -1 0 -1 0 1 0/cl;
ods output LSMEstimates
run; quit;
%mend;
%do_regression(original total domain1)
Here is example of data structure:
Question: I am new to SAS macros and am working with a SAS macro code to run the following regression model for three outcome variables (original total domain1). I output the results using: ods output LSMEstimates which created three datasets named data1—data3 with the estimates. However, I cannot figure out how to attach the outcome variable names of these datasets. Eventually, I would only want the following to be stored in one final dataset that can “set” data1—data3: effect label estimate lower upper. [I only want to store estimates from the two lsmestimate statements shown that I am outputting using: ods output LSMEstimates]

To aggregate the datasets you can use PROC APPEND.
ods output LSMEstimates=lsm;
run;quit;
proc append data=lsm base=lsm_aggregate force;
run;
If the value/variable &DEP is not already in the dataset generated by the ODS OUTPUT statement then add a step to add it.
data lsm_dep ;
length dep $32 ;
dep = "&dep";
set lsm;
run;
proc append data=lsm_dep base=lsm_aggregate force;
run;
Make sure to remove the LSM_AGGREGATE dataset before running a new batch of models.
proc delete data=lsm_aggregate; run;
%do_regression(original )
%do_regression(total )
%do_regression(domain1)

Related

PROC MEANS output as table

I'm trying to export quartile information on a grouped dataset as a dataset in SAS but when I run this code my output is a table with the correct information displayed but the dataset WORK.TOP_1O_PERC is only summary statistics of the set (no quartiles). Does anyone know how I can export this as the CLASS (PDX) and its 25th and 75th percentiles? Thanks!
PROC MEANS DATA=WORK.TOP_10_DX P25 P75;
CLASS PDX;
VAR AmtPaid;
OUTPUT OUT = WORK.TOP_10_PERC;
RUN;
I like the STACKODS output that is a data set which is like the default printed output.
proc means data=sashelp.class n p25 p75 stackods;
ods output summary=summary;
run;
proc print;
run;
You can use output statement with <statistics>= options.
PROC MEANS DATA=WORK.TOP_10_DX NOPRINT;
CLASS PDX;
VAR AmtPaid;
OUTPUT OUT = WORK.TOP_10_PERC P25=P25 P75=P75;
RUN;
Compared to ods output, output statement is much faster but less flexible with multiple analysis variables or by statement specified situation.

VARMAX modelling in SAS

I want to know what the difference is when inserting variables in proc reg and then forecast the residuals with VARMAX
and
inserting the significant variables from proc reg to the VARMAX modelling.
In code:
Proc reg data=x printall;
dependent_variable=exogenous_variable1 exogenous_variable2 ... exogenous_variablep
white vif tol dw dwprob selection=b slentry=0.05;
output out=xforecast Rstudent=Rstudent student=student covratio=covratio h=h predicted=pred residual=residual;
Proc varmax data=xforecast
model residual/
p=7 q=2
method=ls dftest minic=(type=aic) print=(roots);
nloptions;
garch p=1 q=1 form=ccc;
output out=forecast1 lead=21;
run;
Or with only VARMAX:
proc varmax data=xforecast printall;
dependent_variable=exogenous_variable1 exogenous_variable2 ... exogenous_variablep/
p=7 q=2 method=ls dftest minic=(type=aic) print=(roots);
nloptions;
garch p=1 q=1 form=ccc;
output out=forecast1 lead=21;
With only running VARMAX it is also not possible to generate a forecast which i don't understand. The error code:
WARNING: The value of LEAD=21 in OUTPUT statement. There are only 0 future independent observations. The value of LEAD will take
the minimum of two values.

Regression results returning 0 in SAS Enterprise Guide

I am brand new to SAS . I'm trying to create dummy variables in SAS and run regression. After doing some research, I put the following codes together, but the results are showing 0 for a few Col. Does anyone know how to fix this? Thanks !
proc glmmod data = Work.Cost outdesign= GLMDesign outparm=GLMParm;
class sex age Membership; model amount=sex age ;
run;
proc print data=GLMDesign ; run; proc print data=GLMParm ; run;
proc reg data=GLMDesign; DummyVars: model amount = COL1-COL69; ods select ParameterEstimates;
quit;

Average number of rows per variable in SAS

I have the following dataset :
data test;
input business_ID $;
datalines;
'busi1'
'busi1'
'busi1'
'busi2'
'busi3'
'busi3'
;
run;
proc freq data = test ;
table business_ID;
run;
I would like the average nummber of lines per business, that is count the total number of observations and divide it by the number of distinct businesses.
In my example : 6 observations, 3 businesses -> 6/2=3 lines per business.
I was thinking about using a proc freq or a proc mean step but so far I got only the number of lines (~freq) per business and do not know how to get to my goal.
Any idea?
You could use PROC FREQ to get the counts and then run PROC MEANS on the output.
proc freq data=test ;
tables business_id / noprint out=counts ;
run;
proc means data=counts;
var count;
run;
Or you could count them directly with PROC SQL code.
proc sql ;
select count(*)/count(distinct business_id) as mean_count
from test
;
quit;

Macro not loading data set

Previous Posts :
Variable check and summary out
Macro that outputs table with testing results of SAS table
Question/Problem
From the previous posts, I thought I was able to run the macro and produce the desired results. However, after finally getting a report back that the output is not working I'm really confused as to why I'm getting the error that there were missing variables. It appears as if the data set is not being loaded after sub-setting. I'm able to process basic summary statistic tables, but when I load the macro the output is not working.
Why is the data set not loading? Does a macro require a certain type of data set?
Note : A limitation is that I do not have access to the data set, so I must send code to be run and won't get results for a few days. It's a very long and frustrating process, but I'm sure some can relate.
The code that is causing problems is the macro (in beginning of code) and the very last section which calls the macro with the data set.
Error Log :
Code :
# Filename : Census2007_Hawaii_BearingCoffee_BigIsland.sas
/******************************************************************
Clearance Test Macro
input_dataset - desired dataset which variables are located
output_dataset - an output table with test results
variable_to_consider - list of variables to compute test on
*******************************************************************/
%macro clearance_test(input_dataset= ,output_dataset=, variable_to_consider=);
%let variable_to_consider=%cmpres(&variable_to_consider);
proc sql noprint;
select count(*) into : obs_count from &input_dataset;
quit;
%let obs_count=&obs_count;
proc transpose data=&input_dataset out=&output_dataset prefix=top_;
var &variable_to_consider;
run;
data &output_dataset;
set &output_dataset end=eof;
array top(*) top_&obs_count.-top_1;
x=dim(top);
call sortn(of top[*]);
total=sum(of top[*]);
top_2_total=sum(top_1, top_2);
if sum(top_1,top_2) > 0.9 * total then Flag90=1; else Flag90=0;
if top_1 > total * 0.6 then Flag60=1; else Flag60=0;
keep total top_1 top_2 _name_ top_2_total total Flag60 Flag90;
run;
%mend mymacro;
/***********************************************************************/
*Define file path statics;
Libname def 'P:\Hawaii_Arita\John_Hawaii_Coffee\Datasets';
Libname abc "P:\Hawaii_Arita\John_Hawaii_Coffee\Datasets";
option obs=max;
/* Initialize database */
DATA def.Census2007_Hawaii_Coffee;
SET abc.census2007_hawaii_SubSet_Coffee;
**<create the variables used in the macro> **;
RUN;
/* Clearance Test Results */
%clearance_test(input_dataset=def.census2007_hawaii_SubSet_Coffee, output_dataset=test_data ,variable_to_consider= OIR OIRO ROA ROAO SProfit
LProfit SProfitAcre LProfitAcre Profitable MachineandRent UtilityandFuel LaborH LaborO FertilizerandChem MaintandCustom
Interest Tax Dep Others TFPE_cal operators workers operatorsandworkers)
A Complete/Verifiable Example :
This has been tested on the remote machine and works perfectly.
/* Create test data set*/
data business_data;
do firm = 1 to 3;
revenue = rand("uniform");
costs = rand("uniform");
profits = rand("uniform");
vcost = rand("uniform");
output;
end;
run;
/******************************************************************
Clearance Test Macro
input_dataset - desired dataset which variables are located
output_dataset - an output table with test results
variable_to_consider - list of variables to compute test on
*******************************************************************/
%macro clearance_test(input_dataset= ,output_dataset=, variable_to_consider=);
%let variable_to_consider=%cmpres(&variable_to_consider);
proc sql noprint;
select count(*) into : obs_count from &input_dataset;
quit;
%let obs_count=&obs_count;
proc transpose data=&input_dataset out=&output_dataset prefix=top_;
var &variable_to_consider;
run;
data &output_dataset;
set &output_dataset end=eof;
array top(*) top_&obs_count.-top_1;
x=dim(top);
call sortn(of top[*]);
total=sum(of top[*]);
top_2_total=sum(top_1, top_2);
if sum(top_1,top_2) > 0.9 * total then Flag90=1; else Flag90=0;
if top_1 > total * 0.6 then Flag60=1; else Flag60=0;
keep total top_1 top_2 _name_ top_2_total total Flag60 Flag90;
run;
%mend mymacro;
/* Print summary table, run macro, and print clearance test table */
PROC MEANS data = business_data n sum mean median std;
VAR revenue costs profits vcost;
RUN;
%clearance_test(input_dataset=business_data, output_dataset=test_data ,
variable_to_consider=revenue costs profits vcost)
proc print data = test_data; run;
This is where a minimal, complete verifiable example (MCVE) would be helpful for testing whether your problem is a problem with the code, or the data.
Here's the code above, but with a SASHELP dataset (those are built-in to SAS so everyone has them).
%macro clearance_test(input_dataset= ,output_dataset=, variable_to_consider=);
%let variable_to_consider=%cmpres(&variable_to_consider);
proc sql noprint;
select count(*) into : obs_count from &input_dataset;
quit;
%let obs_count=&obs_count;
proc transpose data=&input_dataset out=&output_dataset prefix=top_;
var &variable_to_consider;
run;
data &output_dataset;
set &output_dataset end=eof;
array top(*) top_&obs_count.-top_1;
x=dim(top);
call sortn(of top[*]);
total=sum(of top[*]);
top_2_total=sum(top_1, top_2);
if sum(top_1,top_2) > 0.9 * total then Flag90=1; else Flag90=0;
if top_1 > total * 0.6 then Flag60=1; else Flag60=0;
keep total top_1 top_2 _name_ top_2_total total Flag60 Flag90;
run;
%mend clearance_test;
%clearance_test(input_dataset=sashelp.cars, output_dataset=work.test, variable_to_consider=mpg_city mpg_highway);
That's the exact macro, just using a different input dataset. It works correctly on my machine (the flag variables are meaningless since the data isn't right for them, but the code works).
Run the same on your colleague's machine, and if it runs, then you know the data is the problem (ie, the dataset doesn't have the variables you think it does). If it doesn't run, then you have some other problem (perhaps an issue with how it's being submitted, maybe you end up with spurious characters or something).