DATA Step, Macro, Functions and more

Using the data step PUT function in a macro DO loop

Reply
Occasional Contributor
Posts: 17

Using the data step PUT function in a macro DO loop

Hello all,
I've been trying to get the following program working without much success. Essentially, I want to run a macro %do loop with a data step WHERE statement to produce a custom report. Here is the code (note the format fxx) :
option symbolgen mprint mlogic;

proc format;

value fxx 1 = 'Cold'
2 = 'Cool'
3 = 'Tepid'
4 = 'Warm'
5 = 'Hot';

option formdlim = ' ';

data one;
input x;
datalines;
1
2
2
3
5
;

%macro runit;
%do i = 1 %to 5;
proc print data = one;
where x = &i;
format x fxx.;
title "Data set ONE from Macro when index is put((&i),fxx.))";
run;
%end;
%mend runit;

%runit

What I want is each where case to procduce the title :
Data set ONE from Macro when index is tepid
Instead SAS either displays the actual character string 'put(3,fxx) 'or says it can't
find the put function.
I can get the macro to run if I use :
title 'Data set ONE with index &i - but then it displays just the number (e.g. 3), not the 'mapped' value (e.g. 'tepid').

I tried %sysfunc, %eval, and even writing a macro to translate the number on the title statement - without success.
Any ideas here ?
Barry Walton
Barry.Walton@millersville.edu
Any ideas here ?
Regular Contributor
Posts: 241

Re: Using the data step PUT function in a macro DO loop

Only if you insist on using macros...
[pre]
proc format;
value temp 1 = "Cold" 2 = "Cool" 3 = "Tepid"
4 = "Warm" 5 = "Hot";
run;

data one;
input x @@;
cards;
1 2 2 3 5
;
run;

%macro printf(x=);
%local xf;
%let xf = %sysfunc(putn(&x, temp.));
title "Data set one from macro where x=&xf";
proc print data=one;
where x = &x;
format x temp.;
run;
title;
%mend printf;

%macro runit();
%local i;
%do i = 1 %to 5;
%printf(x=&i)
%end;
%mend runit;

%runit()
[/pre]
Super Contributor
Super Contributor
Posts: 3,174

Re: Using the data step PUT function in a macro DO loop

The OP's SAS program is solid except for the TITLE statement, as shown in post-reply above - the %SYSFUNC(...) needs to be used.

Scott Barry
SBBWorks, Inc.
Occasional Contributor
Posts: 17

Re: Using the data step PUT function in a macro DO loop

All,

The solution given by chang_y_chung worked like the proverbial charm ! The key was declaring xf as a local macro variable and using this statement within a macro to assign the appropriate value :
%let xf = %sysfunc(putn(&x, temp.));
Thanks to all who responded !
Barry Walton
Respected Advisor
Posts: 3,777

Re: Using the data step PUT function in a macro DO loop

Maybe you don't need macros at all.

[pre]
options byline=0;
title "Data set ONE from Macro when index is #byval1";
proc print data = one;
by X;
var x;
format x fxx.;
run;
options byline=1;
[/pre]
Ask a Question
Discussion stats
  • 4 replies
  • 150 views
  • 0 likes
  • 4 in conversation