BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

Hello

I  want to perform multiple proc freq using Macro.

I want that in each explanatory variable the categories will be by formatted order.

When I run the following code I get an error.

I will be happy to learn what is wrong with this code and to see the correct code.

 

proc format;
value $Fm1t ( notsorted)
'Male'='M'
'Female'='F';
Run;

proc format;
value $Fm2t ( notsorted)
'Desirable'='Desirable'
'Borderline'='Borderline'
'High'='High';
Run;

proc format;
value $Fm3t ( notsorted)
'Underweight'='Underweight'
'Normal'='Normal weight'
'Overweight'='Overweight';
Run;

proc format;
value $Fm4t ( notsorted)
'Non-smoker'='Non-smoker'
'Light (1-5)'='Light Smoker (1-5)'
'Moderate (6-15)'='Moderate Smoker(6-15)'
'Heavy (16-25)'='Heavy Smoker(16-25)'
'Very Heavy (> 25)'='Very Heavy Smoker(> 25)';
Run;

proc format;
value $Fm5t ( notsorted)
'Optimal'='Optimal Blood Pressure'
'Normal'='Normal Blood Pressure'
'High'='High Blood Pressure';
Run;

Data heart;
set sashelp.heart;
Format sex $Fm1t.
Chol_Status $Fm2t.
Weight_Status $Fm3t.
Smoking_Status $Fm4t.
BP_Status $Fm5t.;
Run;



%macro mmacro1(pred,i); 
proc freq data=heart order=formatted;  
tables &pred*Status/chisq sparse outpct out=outfreq&i ;
output out=stats&i chisq;
format &pred $Fm&i.t;
run;
%mend;

%mmacro1(sex,1);
%mmacro1(Chol_Status,2);
%mmacro1(Weight_Status,3);
%mmacro1(Smoking_Status,4);
%mmacro1(BP_Status,5);

 

5 REPLIES 5
PaigeMiller
Diamond | Level 26

When I run the following code I get an error.

 

What error? Show us the full SASLOG.

--
Paige Miller
Ronein
Meteorite | Level 14

IF you run the code you will see the log.

"

NOTE: Line generated by the macro variable "I".
3840 $Fm1t
-
22
200
WARNING: Variable FM1T not found in data set WORK.HEART.

ERROR 22-322: Syntax error, expecting one of the following: a name, a format name, ;, -, :, _ALL_, _CHARACTER_, _CHAR_, _NUMERIC_.

ERROR 200-322: The symbol is not recognized and will be ignored."

andreas_lds
Jade | Level 19

In the line

format &pred $Fm&i.t;

you need another dot after the t:

 

format &pred $Fm&i.t.;

 

 

Ronein
Meteorite | Level 14

Perfect and thank you.

Can you please explain Why the sort is not working well? I want that categories will appear in same order as they appear in proc format.( frequencies in order as determined by the formatted value)

 

Joe

 

ballardw
Super User

@Ronein wrote:

Perfect and thank you.

Can you please explain Why the sort is not working well? I want that categories will appear in same order as they appear in proc format.( frequencies in order as determined by the formatted value)

 

Joe

 


Similar to your other question on formatted order: Proc Freq uses the Formatted VALUE, not the order of appearance in a format definition to order the output.

So in these format definitions:

proc format;
value $Fm1t ( notsorted)
'Male'='M'
'Female'='F';  F is before M 
Run;

proc format;
value $Fm2t ( notsorted)
'Desirable'='Desirable'
'Borderline'='Borderline' Borderline is before Desirable
'High'='High';
Run;

proc format;
value $Fm3t ( notsorted)
'Underweight'='Underweight'
'Normal'='Normal weight'  Normal is before Underweight
'Overweight'='Overweight';
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
  • 5 replies
  • 1138 views
  • 0 likes
  • 4 in conversation