I am trying to write the code such that whenever the country count is LE to the freq parameter then the required continent format should be applied to the particular country. below is my code please give some suggestions to get the desired output.
here below am to trying to assign the format to the variable whose frequency is equal or less to the freq parameter but instead is displaying this error----->
Line generated by the macro variable "VARIABLE".
1 country
-------
73
76
ERROR 73-322: Expecting an =.
ERROR 76-322: Syntax error, statement will be ignored
DATA .COUNTRY;
input country $25.;
CARDS;
INDIA
INDIA
INDIA
INDIA
SPAIN
SPAIN
SPAIN
SPAIN
GERMANY
GERMANY
GERMANY
GERMANY
;
run;
%macro Country_Check (indsn=, variable=, outdsn=,freq=);
PROC SORT DATA=&indsn;
by &variable;
run;
PROC FREQ DATA=&indsn;
TABLES &variable / noprint out=counts (KEEP=&variable count);
run;
proc format;
value $city 'INDIA'='ASIA'
'GERMANY'='EUROPE'
'SPAIN'='EUROPE'
'BELGIUM'='EUROPE'
'FRANCE'='EUROPE'
other='UNKNOWN';
data &outdsn;
MERGE &indsn counts;
By &variable;
if count <= &Freq then do
format &variable $city.;
end;
DROP count;
run;
proc print data = &outdsn; run;
%MEND Country_Check;
%Country_Check (indsn=myfile.COUNTRY, variable=country, outdsn=want1, Freq=4);
And a do statement must be terminated with a semicolon.
It is always helpful to run the code that shall go into a macro without the macro definition first, to check that you get correct SAS code with the supplied parameters:
DATA COUNTRY;
input country $25.;
CARDS;
INDIA
INDIA
INDIA
INDIA
SPAIN
SPAIN
SPAIN
SPAIN
GERMANY
GERMANY
GERMANY
GERMANY
;
run;
*%macro Country_Check (indsn=, variable=, outdsn=,freq=);
%let indsn=country;
%let variable=country;
%let outdsn=want1;
%let freq=4;
PROC SORT DATA=&indsn;
by &variable;
run;
PROC FREQ DATA=&indsn;
TABLES &variable / noprint out=counts (KEEP=&variable count);
run;
proc format;
value $city 'INDIA'='ASIA'
'GERMANY'='EUROPE'
'SPAIN'='EUROPE'
'BELGIUM'='EUROPE'
'FRANCE'='EUROPE'
other='UNKNOWN';
run;
data &outdsn;
MERGE &indsn counts;
By &variable;
if count <= &Freq then do;
newvar= put(&variable, $city.);
end;
DROP count;
run;
proc print data = &outdsn; run;
*%MEND Country_Check;
*%Country_Check (indsn=myfile.COUNTRY, variable=country, outdsn=want1, Freq=4);
This will work. You can proceed from there.
You cannot assign a format dynamically. A column has a format, or it doesn't. Putting a format statement into an if block is useless, as the format assignment takes place when the data step is compiled and the PDV is created.
You can conditionally use the put function to create a new character variable like
if (invar has specific value)
then outvar=put(invar,format_a.);
else outvar=put(invar,format_b.);
Am still checking what goes wrong inside your macro.
Your initial data step won't work. Dataset names MUST NOT start with a dot.
And a do statement must be terminated with a semicolon.
It is always helpful to run the code that shall go into a macro without the macro definition first, to check that you get correct SAS code with the supplied parameters:
DATA COUNTRY;
input country $25.;
CARDS;
INDIA
INDIA
INDIA
INDIA
SPAIN
SPAIN
SPAIN
SPAIN
GERMANY
GERMANY
GERMANY
GERMANY
;
run;
*%macro Country_Check (indsn=, variable=, outdsn=,freq=);
%let indsn=country;
%let variable=country;
%let outdsn=want1;
%let freq=4;
PROC SORT DATA=&indsn;
by &variable;
run;
PROC FREQ DATA=&indsn;
TABLES &variable / noprint out=counts (KEEP=&variable count);
run;
proc format;
value $city 'INDIA'='ASIA'
'GERMANY'='EUROPE'
'SPAIN'='EUROPE'
'BELGIUM'='EUROPE'
'FRANCE'='EUROPE'
other='UNKNOWN';
run;
data &outdsn;
MERGE &indsn counts;
By &variable;
if count <= &Freq then do;
newvar= put(&variable, $city.);
end;
DROP count;
run;
proc print data = &outdsn; run;
*%MEND Country_Check;
*%Country_Check (indsn=myfile.COUNTRY, variable=country, outdsn=want1, Freq=4);
This will work. You can proceed from there.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.