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

Hey guys,
So my trouble here is that i want to do a chi² on two different data set. The first one look like a matrix of 128^i value and the second of a vektor. 
I would like to do something like :
proc freq data=WORK.narrb&t;
          tables col[i]_of_WORK.narrb&t * work.vectb&q. /chisq;
run; 

 

And so i want to do this calcul for each column of my matrix with the data set of vector. 
This is my code so far : 

MLOGIC(MAIN):  %DO loop index variable H is now 6; loop will not iterate again.
SYMBOLGEN:  Macro variable X resolves to 2
MLOGIC(MAIN):  %DO loop beginning; index variable I; start value is 1; stop value is 2; by value is 1.  
SYMBOLGEN:  Macro variable T resolves to 1
MPRINT(MAIN):   proc freq data=WORK.narrb1;
SYMBOLGEN:  Macro variable T resolves to 1
SYMBOLGEN:  Macro variable Q resolves to 2
NOTE 138-205: Line generated by the macro variable "T".
192    work.narrb1
       -----------
       22
ERROR 22-322: Syntax error, expecting one of the following: a name, (, _ALL_, _CHARACTER_, _CHAR_, _NUMERIC_.  
NOTE: Line generated by the macro variable "T".
192    work.narrb1
       -----------
       201
ERROR 201-322: The option is not recognized and will be ignored.
ERROR: Variable NAME not found.
MPRINT(MAIN):   tables work.narrb1 * work.vectb2 /chisq;
MPRINT(MAIN):   run;
NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE FREQ used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
      
SYMBOLGEN:  Macro variable T resolves to 1
MPRINT(MAIN):   proc freq data=WORK.narrt1;
SYMBOLGEN:  Macro variable T resolves to 1
SYMBOLGEN:  Macro variable Q resolves to 2
NOTE 138-205: Line generated by the macro variable "T".
192    work.narrb1
       -----------
       22
ERROR 22-322: Syntax error, expecting one of the following: a name, (, _ALL_, _CHARACTER_, _CHAR_, _NUMERIC_.  
NOTE: Line generated by the macro variable "T".
192    work.narrb1
       -----------
       201
ERROR 201-322: The option is not recognized and will be ignored.
ERROR: Variable NAME not found.
MPRINT(MAIN):   tables work.narrb1 * work.vectt2 /chisq;
MPRINT(MAIN):   run;
NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE FREQ used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
      
MLOGIC(MAIN):  %DO loop index variable I is now 2; loop will iterate again.
SYMBOLGEN:  Macro variable T resolves to 1
MPRINT(MAIN):   proc freq data=WORK.narrb1;
SYMBOLGEN:  Macro variable T resolves to 1
SYMBOLGEN:  Macro variable Q resolves to 2
NOTE 138-205: Line generated by the macro variable "T".
192    work.narrb1
       -----------
       22
ERROR 22-322: Syntax error, expecting one of the following: a name, (, _ALL_, _CHARACTER_, _CHAR_, _NUMERIC_.  
NOTE: Line generated by the macro variable "T".
192    work.narrb1
       -----------
       201
ERROR 201-322: The option is not recognized and will be ignored.
ERROR: Variable NAME not found.
MPRINT(MAIN):   tables work.narrb1 * work.vectb2 /chisq;
MPRINT(MAIN):   run;
NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE FREQ used (Total process time):
      real time           0.00 seconds
      cpu time            0.01 seconds
      
SYMBOLGEN:  Macro variable T resolves to 1
MPRINT(MAIN):   proc freq data=WORK.narrt1;
SYMBOLGEN:  Macro variable T resolves to 1
SYMBOLGEN:  Macro variable Q resolves to 2
NOTE 138-205: Line generated by the macro variable "T".
192    work.narrb1
       -----------
       22
ERROR 22-322: Syntax error, expecting one of the following: a name, (, _ALL_, _CHARACTER_, _CHAR_, _NUMERIC_.  
NOTE: Line generated by the macro variable "T".
192    work.narrb1
       -----------
       201
ERROR 201-322: The option is not recognized and will be ignored.
ERROR: Variable NAME not found.
MPRINT(MAIN):   tables work.narrb1 * work.vectt2 /chisq;
MPRINT(MAIN):   run;
NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE FREQ used (Total process time):
      real time           0.00 seconds
      cpu time            0.01 seconds
      
MLOGIC(MAIN):  %DO loop index variable I is now 3; loop will not iterate again.
MLOGIC(MAIN):  Ending execution.
MPRINT(REPETION):  ;
options mprint symbolgen mlogic;

%macro repetion;
	/*  Cette fonction sert à parcourir
	tous mes fichiers*/

	%global t;
	%global q;

	/* Variable taille matrice*/
	%global carac;

	/* Variable type matrice */
	%global type;

	%do t=1 %to 3;

	/* On importe ici les fichiers narr de taille */ 
		FILENAME REFFILE FILESRVC 
	FOLDERPATH="/Users/olivier.perruchet@gmail.com/Zebrys/BDD/test" 
	FILENAME="integ_narr_&t._unif_bina_1.txt";
		%let name= "integ_narr_&t._unif_bina._1.txt";

		PROC IMPORT DATAFILE=REFFILE DBMS=CSV OUT=WORK.narrb&t;
			GETNAMES=no;
			DATAROW=1;
		RUN;

		PROC CONTENTS DATA=WORK.narrb&t;
		RUN;


	
		FILENAME REFFILE FILESRVC 
	FOLDERPATH="/Users/olivier.perruchet@gmail.com/Zebrys/BDD/test" 
	FILENAME="integ_vect_&t._unif_tiny_1.txt";
		%let name= "integ_narr_&t._unif_tiny_1.txt";

		PROC IMPORT DATAFILE=REFFILE DBMS=CSV OUT=WORK.narrt&t;
			GETNAMES=no;
			DATAROW=1;
		RUN;

	/* Ici on importe les fichiers vect de taille i+1*/
		%let q= %eval(&t+1); /* On associe narr_i avec vect_i+1*/
		FILENAME REFFILE FILESRVC 
	FOLDERPATH="/Users/olivier.perruchet@gmail.com/Zebrys/BDD/test" 
	FILENAME="integ_vect_&q._unif_bina_1.txt";
		%let name= "integ_narr_&q._unif_bina_1.txt";

		PROC IMPORT DATAFILE=REFFILE DBMS=CSV OUT=WORK.vectb&q;
			GETNAMES=no;
			DATAROW=1;
		RUN;

		PROC CONTENTS DATA=WORK.vectb&q;
		RUN;


		FILENAME REFFILE FILESRVC 
	FOLDERPATH="/Users/olivier.perruchet@gmail.com/Zebrys/BDD/test" 
	FILENAME="integ_vect_&q._unif_tiny_1.txt";
		%let name= "integ_vect_&q._unif_tiny_1.txt";

		PROC IMPORT DATAFILE=REFFILE DBMS=CSV OUT=WORK.vectt&q;
			GETNAMES=no;
			DATAROW=1;
		RUN;

		PROC CONTENTS DATA=WORK.vectt&q;
		RUN;
		%let x = 2;

		/* Notre variable itérateur  */
		%macro main;

		%if &t=1 %then	/* Boucle qui sert à supprimer les valeurs aberrantes */
				%do;

					%do h=1 %to 5;

						proc means data=work.narrb&t mean;
						run;

						proc means data=work.narrb&t median;
						run;

						proc means data=work.narrb&t std;
						run;

						proc means data=work.narrb&t N;
						run;

					%end;
				%end;

		%DO i=1 %to &x;
			proc freq data=WORK.narrb&t;
				tables work.narrb&t.   * work.vectb&q. /chisq;
			run;
			proc freq data=WORK.narrt&t;
				tables work.narrb&t. * work.vectt&q. /chisq;
			run;
		%end;
		%mend;
		%main;

	%end;

%mend;
%repetion;
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Show the code you attempt to use to "merge".

Note that if both sets have the same variables and you use a data step MERGE statement the result will not have any new variables:

data ex1;
   input x y;
datalines;
1 2
2 3
4 5
;

data ex2;
   input x y;
datalines;
1 3
3 5
;

/* same variables in result*/
data merged;
   merge ex1 ex2;
   by x;
run;
/* to change the y from the second set*/
data merged2;
   merge ex1 ex2 (rename=(y=y2));
   by x;
run;

You might be thinking of needing something like the Merged2 set.

If you have multiple sets you could match them all by the same variable, such as the By X above, and rename the Y for each set:

data mergedmany;
  merge ex1 
            ex2 (rename=(y=y2))
            ex3 (rename=(y=y3))
            ex4 (rename=(y=y4))
  ;
  by x;
run;

However if you multiple values for X in multiple data sets the result may not quite be what you want.

View solution in original post

4 REPLIES 4
ballardw
Super User

Some generic suggestions:

1) Separate Reading data from Analysis inside a single macro. Get the data read correctly and verify before moving to analyis. Proc import with many files is often a ticket to failures as variables become different types and if you want to combine data many issues can arise.

 

2) Do not define a macro to be compiled within another macro. At best it is inefficient use of CPU cycles at worst you can end up with some very hard to trace debugging issues when macro values get wonky.

 

3) learn the syntax for things.

You use multiple calls to Proc means such as

proc means data=work.narrb&t mean;
run;

where you could use

proc means data=work.narrb&t mean median n std;
run;

4) If you want to create a group of related variables so you can address them with anything resembling i= 1, 2,3 then use the "i" as a suffix at the end of the variable name and not in the middle. Your coding will be much easier in general and for lists you can very frequently use var1 - var5. Such as in a Tables statement of Proc freq: Tables (var1 - var5) * yvar; will create analysis for Var1*yvar, Var2*yvar, Var3*yvar, Var4*yvar and Var5*yvar. If you need the same analysis crossing the Var1 to Var5 with multiple other variables then you can use parentheses to provide more analysis: Tables (var1-var5) *(yvar zvar qvar) / <options>; where the analysis would have all of the variables in the first group crossed with the second group. CAUTION: This will not work if using the OUT= option to create a data set of the frequencies. But you can use ODS OUTPUT to get all of the

resulting frequencies into a data set with indicators as to which variables are involved.

 

5) Fix macro errors starting at the top:

SYMBOLGEN:  Macro variable T resolves to 1
MPRINT(MAIN):   proc freq data=WORK.narrb1;
SYMBOLGEN:  Macro variable T resolves to 1
SYMBOLGEN:  Macro variable Q resolves to 2
NOTE 138-205: Line generated by the macro variable "T".
192    work.narrb1

which is an example of why I said to separate the read and analysis. You are attempting to use a data set that does not exist. Which means your analysis loop is using elements that may be different than creating your data.

 

And a last element. If the files you are using Proc Import on are supposed to have the same structure you will be better off writing a data step to read the file than relying on proc import.

 

boubou31
Calcite | Level 5

Thanks for those advice it will be really helpful. In fact i don't need the result of the calcul, all i want to do at the end of the day it's to count the time it takes to SAS to do those calculs.

A solution i'm thinking of is to fusion those two tables by using a merge, like to add a new column at my matrix. 
To have a matrix who will have 128 + 1 column, and then do : 
 Proc freq: Tables (var1 - var128) * VektorVar;

But when i try to merge those tables, it didn't add a new column as i expected and hoped.

ballardw
Super User

Show the code you attempt to use to "merge".

Note that if both sets have the same variables and you use a data step MERGE statement the result will not have any new variables:

data ex1;
   input x y;
datalines;
1 2
2 3
4 5
;

data ex2;
   input x y;
datalines;
1 3
3 5
;

/* same variables in result*/
data merged;
   merge ex1 ex2;
   by x;
run;
/* to change the y from the second set*/
data merged2;
   merge ex1 ex2 (rename=(y=y2));
   by x;
run;

You might be thinking of needing something like the Merged2 set.

If you have multiple sets you could match them all by the same variable, such as the By X above, and rename the Y for each set:

data mergedmany;
  merge ex1 
            ex2 (rename=(y=y2))
            ex3 (rename=(y=y3))
            ex4 (rename=(y=y4))
  ;
  by x;
run;

However if you multiple values for X in multiple data sets the result may not quite be what you want.

boubou31
Calcite | Level 5
Thanks you so much, before your example i couldn't get the synthax of merge properly ! As you said the Merged2 set works for me.

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 486 views
  • 1 like
  • 2 in conversation