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

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);

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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.

 

 

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

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.

Kurt_Bremser
Super User

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.

 

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

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

View all other training opportunities.

Discussion stats
  • 3 replies
  • 1383 views
  • 0 likes
  • 2 in conversation