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

Hello programmers,

 

I am trying to run a uni-variate procedure on a macro in which the input dataset and the output out= dataset needs to be a macro variable.

 

I want to run the macro for the whole data set and then for the dataset excluding a particular ID.

 

Please assume that my two datasets are 'one' and 'two', i will be glad for someone to help me check my syntax to see if it's correct and follows the rules for writing macros.

 

%macro Univariate (indata, outdata, run); 
proc univariate data=&indata;
by grouporder groupname testcode testname;
var baseline _12_months change preslope postslpe slopedif;
output out=&outdata;
N= N_baseline N_12_months N_change N_preslope N_postslpe N_slopedif
Mean= Mean_baseline Mean_12_months Mean_change Mean_preslope Mean_postslpe Mean_slopedif
Std= std_baseline std_12_months std_change std_preslope std_postslpe std_slopedif
Probs= prob_baseline prob_12_months prob_change prob_preslope prob_postslpe prob_slopedif;
run;
indata=&indata;
outdata=&outdata;
run=&run;
%mend Univariate; 
%Univariate (one, onea, 2);
%Univariate(two, twoa, 1);
run;
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@ChuksManuel wrote:

Hello programmers,

 

I am trying to run a uni-variate procedure on a macro in which the input dataset and the output out= dataset needs to be a macro variable.

 

I want to run the macro for the whole data set and then for the dataset excluding a particular ID.

 

Please assume that my two datasets are 'one' and 'two', i will be glad for someone to help me check my syntax to see if it's correct and follows the rules for writing macros.

 

%macro Univariate (indata, outdata, run); 
proc univariate data=&indata;
by grouporder groupname testcode testname;
var baseline _12_months change preslope postslpe slopedif;
output out=&outdata;
N= N_baseline N_12_months N_change N_preslope N_postslpe N_slopedif
Mean= Mean_baseline Mean_12_months Mean_change Mean_preslope Mean_postslpe Mean_slopedif
Std= std_baseline std_12_months std_change std_preslope std_postslpe std_slopedif
Probs= prob_baseline prob_12_months prob_change prob_preslope prob_postslpe prob_slopedif;
run;
indata=&indata;
outdata=&outdata;
run=&run;
%mend Univariate; 
%Univariate (one, onea, 2);
%Univariate(two, twoa, 1);
run;

Your semicolon after output=&output terminates the output statement before the statistics requested. Remove it.

%macro Univariate (indata, outdata, run); 
proc univariate data=&indata;
by grouporder groupname testcode testname;
var baseline _12_months change preslope postslpe slopedif;
output out=&outdata;
N= N_baseline N_12_months N_change N_preslope N_postslpe N_slopedif
Mean= Mean_baseline Mean_12_months Mean_change Mean_preslope Mean_postslpe Mean_slopedif
Std= std_baseline std_12_months std_change std_preslope std_postslpe std_slopedif
Probs= prob_baseline prob_12_months prob_change prob_preslope prob_postslpe prob_slopedif;
run;
indata=&indata;
outdata=&outdata;
run=&run;
%mend Univariate; 
%Univariate (one, onea, 2);
%Univariate(two, twoa, 1);
run;

What the heck is the stuff I highlighted in green supposed to do???? Since the statements come after a RUN statement they are not part of the proc univariate and without a proc or data step they are just hanging out causing errors.

 

View solution in original post

9 REPLIES 9
ChuksManuel
Pyrite | Level 9
108  %macro Univari (indata, outdata, run);
109  proc univariate data=&indata;
110  by grouporder groupname testcode testname;
111  var baseline _12_months change preslope postslpe slopedif;
112  output out=&outdata;
113  N= N_baseline N_12_months N_change N_preslope N_postslpe N_slopedif
114  Mean= Mean_baseline Mean_12_months Mean_change Mean_preslope Mean_postslpe Mean_slopedif
115  Std= std_baseline std_12_months std_change std_preslope std_postslpe std_slopedif
116  Probs= prob_baseline prob_12_months prob_change prob_preslope prob_postslpe prob_slopedif;
117  run;
118  indata=&indata;
119  outdata=&outdata;
120  run=&run;
121  %mend Univari;
122  %Univari (one_5included, one_5includedA, 1);
NOTE: Line generated by the invoked macro "UNIVARI".
1     proc univariate data=&indata; by grouporder groupname testcode testname; var baseline _12_months
1  !  change preslope postslpe slopedif; output out=&outdata; N= N_baseline N_12_months N_change
                                                              -
                                                              180
1  ! N_preslope N_postslpe N_slopedif Mean= Mean_baseline

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.ONE_5INCLUDEDA may be incomplete.  When this step was stopped there were 0
         observations and 0 variables.
WARNING: Data set WORK.ONE_5INCLUDEDA was not replaced because this step was stopped.
NOTE: PROCEDURE UNIVARIATE used (Total process time):
      real time           0.03 seconds
      cpu time            0.01 seconds

123  %Univari(one_5excluded,one_5excludedA, 2);


NOTE: Line generated by the invoked macro "UNIVARI".
1     proc univariate data=&indata; by grouporder groupname testcode testname; var baseline _12_months
1  !  change preslope postslpe slopedif; output out=&outdata; N= N_baseline N_12_months N_change
                                                              -
                                                              180
1  ! N_preslope N_postslpe N_slopedif Mean= Mean_baseline

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.ONE_5EXCLUDEDA may be incomplete.  When this step was stopped there were 0
         observations and 0 variables.
WARNING: Data set WORK.ONE_5EXCLUDEDA was not replaced because this step was stopped.
NOTE: PROCEDURE UNIVARIATE used (Total process time):
      real time           0.02 seconds
      cpu time            0.01 seconds

124  run;


PaigeMiller
Diamond | Level 26

Step #1 of getting any macro to work: create WORKING SAS code without macros and without macro variables, in your case for a given input and output data set. You do not have working SAS code, so the macro will fail for the same reason. Once you have working SAS code, then you can turn it into a macro easily; until you have working SAS code, creating a macro is pointless and will be a failure.


So, please create working SAS code for one such input and output data set. We can help you with that if you get stuck.

--
Paige Miller
Reeza
Super User

Remove the semi colon from this line - that's not valid syntax, the semi colon comes at the end of the line.

output out=&outdata;

 

I have no idea what you think this is doing but it likely needs to be deleted:

indata=&indata;
outdata=&outdata;
run=&run;

I also have no idea what you're trying to do with the RUN value there.

 

Start with working code and convert it to a macro using this approach. It works if you follow the steps.

 

https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md

 


@ChuksManuel wrote:

Hello programmers,

 

I am trying to run a uni-variate procedure on a macro in which the input dataset and the output out= dataset needs to be a macro variable.

 

I want to run the macro for the whole data set and then for the dataset excluding a particular ID.

 

Please assume that my two datasets are 'one' and 'two', i will be glad for someone to help me check my syntax to see if it's correct and follows the rules for writing macros.

 

%macro Univariate (indata, outdata, run); 
proc univariate data=&indata;
by grouporder groupname testcode testname;
var baseline _12_months change preslope postslpe slopedif;
output out=&outdata;
N= N_baseline N_12_months N_change N_preslope N_postslpe N_slopedif
Mean= Mean_baseline Mean_12_months Mean_change Mean_preslope Mean_postslpe Mean_slopedif
Std= std_baseline std_12_months std_change std_preslope std_postslpe std_slopedif
Probs= prob_baseline prob_12_months prob_change prob_preslope prob_postslpe prob_slopedif;
run;
indata=&indata;
outdata=&outdata;
run=&run;
%mend Univariate; 
%Univariate (one, onea, 2);
%Univariate(two, twoa, 1);
run;

 

ChuksManuel
Pyrite | Level 9
data one;
input SUBJID age	sex $	race $	Testcode	Testname	npre $	preslope	npost	postslpe	slopedif	Baseline	_12_months	change	grouporder	groupname $;
datalines;	
1	60	F	W	2	UHDRS	4	14.17	5	-4.7257	-18.8982	42	34	-8	1	Motor 	
2	54	M	W	2	UHDRS	5	7.94	5	1.5295	-6.4195	38	32	-6	1	Motor 	
3	64	F	W	2	UHDRS	5	3.59	5	-12.355	-15.9493	33	21	-12	1	Motor 	
4	42	F	W	2	UHDRS	4	1.54	5	3.2296	1.6878	25	24	-1	1	Motor 	
5	59	F	W	2	UHDRS	4	-2.05	4	15.8111	17.8226	27	43	16	1	Motor 	
6	46	F	W	2	UHDRS	4	6.61	5	5.9436	-0.6707	36	30	-6	1	Motor 	
7	28	F	W	2	UHDRS	4	7.24	5	2.7092	-4.5369	29	24	-5	1	Motor 	
1	60	F	W	3	CHOREA	4	1.91	5	-3.9229	-5.8362	16	12	-4	1	Motor 	
2	54	M	W	3	CHOREA	5	4.15	5	-2.7564	-6.9109	14	10	-4	1	Motor 	
3	64	F	W	3	CHOREA	5	1.07	5	-0.8913	-1.9615	12	12	0	1	Motor 	
4	42	F	W	3	CHOREA	4	-1.5535	5	0.5566	2.1101	6	8	2	1	Motor 	
5	59	F	W	3	CHOREA	4	-0.1152	4	-0.9775	-0.8623	10	9	-1	1	Motor 	
6	46	F	W	3	CHOREA	4	0.4563	5	1.6572	1.2009	16	12	-4	1	Motor 	
1	60	F	W	7	BALANCE 	4	0.8466	5	-0.4438	-1.2904	5	5	0	1	Motor 
2	54	M	W	7	BALANCE 	5	-1.2269	5	1.9508	3.1778	4	5	1	1	Motor 
3	64	F	W	7	BALANCE 	5	-1.8719	5	-0.4801	1.3918	3	3	0	1	Motor 	
4	42	F	W	7	BALANCE 	4	1.9256	5	0	-1.9256	2	0	-2	1	Motor 
5	59	F	W	7	BALANCE 	4	-0.4165	4	4.33	4.7465	4	8	4	1	Motor 
6	46	F	W	7	BALANCE	4	0.3149	5	0.9779	0.663	1	2	1	1	Motor
7	28	F	W	7	BALANCE	4	1.6192	5	1.2886	-0.3306	4	2	-2	1	Motor
1	60	F	W	1	TFC	4	-1.4901	5	1.2091	2.6992	5	6	1	2	Physical 
2	54	M	W	1	TFC	5	2.0796	5	0.7828	-1.2968	6	6	0	2	Physical 
3	64	F	W	1	TFC	5	-2.2018	5	0	2.2018	7	7	0	2	Physical 
4	42	F	W	1	TFC	4	0.0235	4	3.4024	3.3789	8	11	3	2	Physical 
5	59	F	W	1	TFC	4	-0.8093	4	-0.7545	0.0548	6	4	-2	2	Physical 
6	46	F	W	1	TFC	4	-2.7852	5	-0.445	2.3401	7	7	0	2	Physical 
7	28	F	W	1	TFC	3	-2.8253	5	-1.2804	1.5449	7	8	1	2	Physical 
1	60	F	W	13	FA	4	1.0244	5	2.4346	1.4102	18	18	0	2	Physical 
2	54	M	W	13	FA	5	-1.677	5	-1.564	0.113	18	18	0	2	Physical 
3	64	F	W	13	FA	5	-0.9935	5	-0.8156	0.178	22	20	-2	2	Physical 
4	42	F	W	13	FA	3	-1.8714	5	1.4583	3.3298	21	23	2	2	Physical 
5	59	F	W	13	FA	4	-1.0633	4	0.1458	1.2091	18	13	-5	2	Physical 
6	46	F	W	13	FA	4	1.3842	5	0.3923	-0.9918	23	21	-2	2	Physical 
7	28	F	W	13	FA	4	-0.9321	5	-3.3786	-2.4465	22	21	-1	2	Physical 
1	60	F	W	14	IS	4	0	5	-0.4958	-0.4958	75	70	-5	2	Physical 	
2	54	M	W	14	IS	5	-6.2295	5	-2.0013	4.2282	70	70	0	2	Physical 
3	64	F	W	14	IS	5	-4.4497	5	0.7619	5.2116	80	75	-5	2	Physical 
4	42	F	W	14	IS	3	-4.6786	5	15.3685	20.0471	75	85	10	2	Physical 
5	59	F	W	14	IS	4	2.0823	4	3.7298	1.6475	70	60	-10	2	Physical 
6	46	F	W	14	IS	4	-4.9759	4	-13.0205	-8.0445	75	70	-5	2	Physical 
7	28	F	W	14	IS	4	11.5312	5	-6.402	-17.9331	95	70	-25	2	Physical 
; run;
Reeza
Super User
You probably shouldn't name your macro the same as a PROC either, that can be confusing. I don't think there's any rule against it, but definitely bad practice.
PaigeMiller
Diamond | Level 26

@ChuksManuel wrote:
data one;
input SUBJID age	sex $	race $	Testcode	Testname	npre $	preslope	npost	postslpe	slopedif	Baseline	_12_months	change	grouporder	groupname $;
datalines;	
1	60	F	W	2	UHDRS	4	14.17	5	-4.7257	-18.8982	42	34	-8	1	Motor 	
2	54	M	W	2	UHDRS	5	7.94	5	1.5295	-6.4195	38	32	-6	1	Motor 	
3	64	F	W	2	UHDRS	5	3.59	5	-12.355	-15.9493	33	21	-12	1	Motor 	
4	42	F	W	2	UHDRS	4	1.54	5	3.2296	1.6878	25	24	-1	1	Motor 	
5	59	F	W	2	UHDRS	4	-2.05	4	15.8111	17.8226	27	43	16	1	Motor 	
6	46	F	W	2	UHDRS	4	6.61	5	5.9436	-0.6707	36	30	-6	1	Motor 	
7	28	F	W	2	UHDRS	4	7.24	5	2.7092	-4.5369	29	24	-5	1	Motor 	
1	60	F	W	3	CHOREA	4	1.91	5	-3.9229	-5.8362	16	12	-4	1	Motor 	
2	54	M	W	3	CHOREA	5	4.15	5	-2.7564	-6.9109	14	10	-4	1	Motor 	
3	64	F	W	3	CHOREA	5	1.07	5	-0.8913	-1.9615	12	12	0	1	Motor 	
4	42	F	W	3	CHOREA	4	-1.5535	5	0.5566	2.1101	6	8	2	1	Motor 	
5	59	F	W	3	CHOREA	4	-0.1152	4	-0.9775	-0.8623	10	9	-1	1	Motor 	
6	46	F	W	3	CHOREA	4	0.4563	5	1.6572	1.2009	16	12	-4	1	Motor 	
1	60	F	W	7	BALANCE 	4	0.8466	5	-0.4438	-1.2904	5	5	0	1	Motor 
2	54	M	W	7	BALANCE 	5	-1.2269	5	1.9508	3.1778	4	5	1	1	Motor 
3	64	F	W	7	BALANCE 	5	-1.8719	5	-0.4801	1.3918	3	3	0	1	Motor 	
4	42	F	W	7	BALANCE 	4	1.9256	5	0	-1.9256	2	0	-2	1	Motor 
5	59	F	W	7	BALANCE 	4	-0.4165	4	4.33	4.7465	4	8	4	1	Motor 
6	46	F	W	7	BALANCE	4	0.3149	5	0.9779	0.663	1	2	1	1	Motor
7	28	F	W	7	BALANCE	4	1.6192	5	1.2886	-0.3306	4	2	-2	1	Motor
1	60	F	W	1	TFC	4	-1.4901	5	1.2091	2.6992	5	6	1	2	Physical 
2	54	M	W	1	TFC	5	2.0796	5	0.7828	-1.2968	6	6	0	2	Physical 
3	64	F	W	1	TFC	5	-2.2018	5	0	2.2018	7	7	0	2	Physical 
4	42	F	W	1	TFC	4	0.0235	4	3.4024	3.3789	8	11	3	2	Physical 
5	59	F	W	1	TFC	4	-0.8093	4	-0.7545	0.0548	6	4	-2	2	Physical 
6	46	F	W	1	TFC	4	-2.7852	5	-0.445	2.3401	7	7	0	2	Physical 
7	28	F	W	1	TFC	3	-2.8253	5	-1.2804	1.5449	7	8	1	2	Physical 
1	60	F	W	13	FA	4	1.0244	5	2.4346	1.4102	18	18	0	2	Physical 
2	54	M	W	13	FA	5	-1.677	5	-1.564	0.113	18	18	0	2	Physical 
3	64	F	W	13	FA	5	-0.9935	5	-0.8156	0.178	22	20	-2	2	Physical 
4	42	F	W	13	FA	3	-1.8714	5	1.4583	3.3298	21	23	2	2	Physical 
5	59	F	W	13	FA	4	-1.0633	4	0.1458	1.2091	18	13	-5	2	Physical 
6	46	F	W	13	FA	4	1.3842	5	0.3923	-0.9918	23	21	-2	2	Physical 
7	28	F	W	13	FA	4	-0.9321	5	-3.3786	-2.4465	22	21	-1	2	Physical 
1	60	F	W	14	IS	4	0	5	-0.4958	-0.4958	75	70	-5	2	Physical 	
2	54	M	W	14	IS	5	-6.2295	5	-2.0013	4.2282	70	70	0	2	Physical 
3	64	F	W	14	IS	5	-4.4497	5	0.7619	5.2116	80	75	-5	2	Physical 
4	42	F	W	14	IS	3	-4.6786	5	15.3685	20.0471	75	85	10	2	Physical 
5	59	F	W	14	IS	4	2.0823	4	3.7298	1.6475	70	60	-10	2	Physical 
6	46	F	W	14	IS	4	-4.9759	4	-13.0205	-8.0445	75	70	-5	2	Physical 
7	28	F	W	14	IS	4	11.5312	5	-6.402	-17.9331	95	70	-25	2	Physical 
; run;

We want you to create working SAS code without macros and without macro variables as the first step of getting a macro to work.

--
Paige Miller
ballardw
Super User

@ChuksManuel wrote:

Hello programmers,

 

I am trying to run a uni-variate procedure on a macro in which the input dataset and the output out= dataset needs to be a macro variable.

 

I want to run the macro for the whole data set and then for the dataset excluding a particular ID.

 

Please assume that my two datasets are 'one' and 'two', i will be glad for someone to help me check my syntax to see if it's correct and follows the rules for writing macros.

 

%macro Univariate (indata, outdata, run); 
proc univariate data=&indata;
by grouporder groupname testcode testname;
var baseline _12_months change preslope postslpe slopedif;
output out=&outdata;
N= N_baseline N_12_months N_change N_preslope N_postslpe N_slopedif
Mean= Mean_baseline Mean_12_months Mean_change Mean_preslope Mean_postslpe Mean_slopedif
Std= std_baseline std_12_months std_change std_preslope std_postslpe std_slopedif
Probs= prob_baseline prob_12_months prob_change prob_preslope prob_postslpe prob_slopedif;
run;
indata=&indata;
outdata=&outdata;
run=&run;
%mend Univariate; 
%Univariate (one, onea, 2);
%Univariate(two, twoa, 1);
run;

Your semicolon after output=&output terminates the output statement before the statistics requested. Remove it.

%macro Univariate (indata, outdata, run); 
proc univariate data=&indata;
by grouporder groupname testcode testname;
var baseline _12_months change preslope postslpe slopedif;
output out=&outdata;
N= N_baseline N_12_months N_change N_preslope N_postslpe N_slopedif
Mean= Mean_baseline Mean_12_months Mean_change Mean_preslope Mean_postslpe Mean_slopedif
Std= std_baseline std_12_months std_change std_preslope std_postslpe std_slopedif
Probs= prob_baseline prob_12_months prob_change prob_preslope prob_postslpe prob_slopedif;
run;
indata=&indata;
outdata=&outdata;
run=&run;
%mend Univariate; 
%Univariate (one, onea, 2);
%Univariate(two, twoa, 1);
run;

What the heck is the stuff I highlighted in green supposed to do???? Since the statements come after a RUN statement they are not part of the proc univariate and without a proc or data step they are just hanging out causing errors.

 

ChuksManuel
Pyrite | Level 9

Thanks guys.

I corrected the error and it worked.

 

ChrisNZ
Tourmaline | Level 20

Changed thread title from  Macro  to  proc univariate does not run

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 9 replies
  • 1682 views
  • 7 likes
  • 5 in conversation