DATA Step, Macro, Functions and more

Display the formatted value

Accepted Solution Solved
Reply
Regular Contributor
Posts: 190
Accepted Solution

Display the formatted value

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


Accepted Solutions
Solution
‎09-21-2016 09:56 AM
Super User
Posts: 7,763

Re: Display the formatted value

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.

 

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Super User
Posts: 7,763

Re: Display the formatted value

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 7,763

Re: Display the formatted value

Your initial data step won't work. Dataset names MUST NOT start with a dot.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Solution
‎09-21-2016 09:56 AM
Super User
Posts: 7,763

Re: Display the formatted value

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.

 

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
☑ This topic is solved.

Need further help from the community? Please ask a new question.

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