BookmarkSubscribeRSS Feed
spg
Obsidian | Level 7 spg
Obsidian | Level 7
I’m new to the macro facility and was trying my hand at writing a code that would give me frequency tables for different products by different years. The code I wrote gives me frequencies for all the products instead of selecting the product and year combinations I specify in the macro. Can anybody please correct the program?
Thanks!

data fruits;
set fruitdata;
if date gt 'XXX'd and date lt 'XXX'd then year= '2007';
if date gt 'XXX'd and date lt 'XXX'd then year= '2008';
if date gt 'XXX'd and date lt 'XXX'd then year= '2009';
if fruit= ‘orange’ then product= '1'; else product ='0';
if fruit= ‘apple’ then product= '2'; else product ='0';
if fruit= ‘pear’ then product= '3'; else product ='0';
run;


%macro test(year=, product=);
proc freq data= fruits;
table fruit;
title ‘Sale of product& in year&';
run;
%mend;

%test(year= '2007', product= '1');
%test(year= '2007', product= '2');
%test(year= '2007', product= '3');
%test(year= '2007', product= '1');
%test(year= '2007', product= '2');
%test(year= '2007', product= '3');
%test(year= '2009', product= '1');
%test(year= '2009', product= '1');
%test(year= '2009', product= '1');
2 REPLIES 2
Cynthia_sas
SAS Super FREQ
Hi:
It's not clear to me where you are trying to use &year and &product...other than in your TITLE statement. You have:
[pre]
title ‘Sale of product& in year&';
[/pre]

... which is incorrect...it is not product& and year&, it should be &product and &year (if &PRODUCT and &YEAR are your macro variables). Also, you have your title statement in SINGLE quotes, which is incorrect, macro variable references only resolve in DOUBLE quotes, as shown in the corrected title statement below.

[pre]
title "Sale of &product in &year";
[/pre]

However, I would also expect you to have a WHERE clause or some other selection criteria in your macro program.

This paper is a good introduction to the SAS Macro facility and may help to clear up some of your confusion on this matter:
http://www2.sas.com/proceedings/sugi28/056-28.pdf

At any rate, I wonder whether you need the macro facility at all for this task. A simple change to your PROC FREQ would work, too. When you use BY group processing, you have access to the BY variables to use in a SAS title statement. So if you did your PROC FREQ with a BY statement (BY year product;), then you could use the special BY variables in the SAS title. No need for macro variables at all. For more information about automatic BY variables in the SAS title, see this:
http://www2.sas.com/proceedings/sugi23/Coders/p75.pdf

cynthia
[pre]
data fruits;
set sashelp.prdsale;
where division = 'CONSUMER';
if country = 'GERMANY' then fruit = 'orange';
else if country = 'CANADA' then fruit = 'apple';
else if country = 'U.S.A.' then fruit = 'pear';

if product in ( 'BED','TABLE') then product = '0';
else if product = 'SOFA' then product = '1';
else if product in ('DESK', 'CHAIR') then product = '2';

if year = 1993 then year = 2007;
else if year = 1994 then year = 2008;
run;

proc sort data=fruits;
by year product;
run;

ods listing;
** Use options NOBYLINE to suppress the automatic BYLINE under the title;
/* options NOBYLINE */
proc freq data= fruits;
by year product;
table fruit;
title "Sale for Year #byval1 and Product #byval2";
run;[/pre]
spg
Obsidian | Level 7 spg
Obsidian | Level 7
You're right...I've muddled things up. I knew the proc reg using "by"...just thought I'd try something different... Thanks for the paper!

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
  • 2 replies
  • 719 views
  • 0 likes
  • 2 in conversation