DATA Step, Macro, Functions and more

Macro

Reply
Contributor
Posts: 38

Macro

I have a SAS dataset created from Proc Freq procedure and transposed. Below is the layout

E.g. Proc Freq;
Tables Sex*year;
Where year ge 2005 and year le 2007;
Run;

Present Output after transposing by sex
Sex Cnt2005 Cnt2006 Cnt2007 Pct2005 Pct2006 Pct2007
Male
Female

Problem: I need to repeat the analysis for other set of years as needed. E.g. instead of 2005-2007 I may need to use 2000 to 2005 in where statement. How do I modify VAR statement in Proc Print

In other words how do I modify the below statement without literally changing them in several proc print statement

VAR sex cnt2005 pct2005 cnt2006 pct2006 cnt2007 pct2007;

Thanks.
Respected Advisor
Posts: 3,889

Re: Macro

Hi

May be below example is a way to go?

data TransposeOutput;
sex=1;
pct2005=25;
cnt2006=50;
pct2007=10;
output;
run;

proc sql;
select name into :varlist separated by ' '
from dictionary.columns
where libname='WORK' and memname=%upcase('TransposeOutput')
;
quit;

proc print data=TransposeOutput;
var &varlist;
run;

HTH
Patrick
Valued Guide
Posts: 2,175

Re: Macro

use a var statement like[pre] var sex cnt&from - cnt&to pct&from - pct&to ;[/pre]where &from and &to are the year range parameters of your macro

luck
peterC
Respected Advisor
Posts: 3,889

Re: Macro

Following Peter's approach you could also use a wildcard as below:

proc print data=TransposeOutput;
var sex pct: cnt:;
run;

This works under the assumption that you always want to print all year-variables in the transposed data set.
Contributor
Posts: 38

Re: Macro

Thanks to all who replied to my post. It worked.
Ask a Question
Discussion stats
  • 4 replies
  • 238 views
  • 0 likes
  • 3 in conversation