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

Hello:

 

I have the following macro codes from the previous programmer who has been gone and no way to find.   Could someone let me know what the 'fmt&i._' refer to and the 'Z' is refer to something?  Also, does the 'Program' in the macro mean it need to run by using the 'data' before the do loop?  Thanks.

 

%macro in(program,z,data);

Libname Workshop "\\Pathway\data";


%include "\\Pathway\&program..sas";

data workshop.&data;
        length LABEL $ 200;
	set %do i=1 %to &z;
	fmt&i._ %end;;
	by FMTNAME START END LABEL;
run;

%mend in;

%in(ABC_Time_check,19,Time_formats);  
%in(ABC_Place_check,113,Place_formats);  

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

When you execute the macro for the first time using

%in(ABC_Time_check,19,Time_formats); 

z would resolve to 19 as that is 2nd value in the positional parameter  in the defining %macro statement

%macro in(program,z,data);

Now, let's move on to find out what %do loop resolves to 

Run this and see the log


%macro t;
%put set;
%do i=1 %to 19;/*z will resolve to 19*/
%put 	fmt&i._ ;
%end; 
%put %str(;);
%mend t;
%t

/*You will notice that loop generating list of dataset names fmt1_  to fmt19_

set 
fmt1_
fmt2_
fmt3_
fmt4_
fmt5_
fmt6_
fmt7_
fmt8_
fmt9_
fmt10_
fmt11_
fmt12_
fmt13_
fmt14_
fmt15_
fmt16_
fmt17_
fmt18_
fmt19_
;

I am sure you know set keyword followed by datsets list is a concatenation/append operation 

View solution in original post

6 REPLIES 6
novinosrin
Tourmaline | Level 20

When you execute the macro for the first time using

%in(ABC_Time_check,19,Time_formats); 

z would resolve to 19 as that is 2nd value in the positional parameter  in the defining %macro statement

%macro in(program,z,data);

Now, let's move on to find out what %do loop resolves to 

Run this and see the log


%macro t;
%put set;
%do i=1 %to 19;/*z will resolve to 19*/
%put 	fmt&i._ ;
%end; 
%put %str(;);
%mend t;
%t

/*You will notice that loop generating list of dataset names fmt1_  to fmt19_

set 
fmt1_
fmt2_
fmt3_
fmt4_
fmt5_
fmt6_
fmt7_
fmt8_
fmt9_
fmt10_
fmt11_
fmt12_
fmt13_
fmt14_
fmt15_
fmt16_
fmt17_
fmt18_
fmt19_
;

I am sure you know set keyword followed by datsets list is a concatenation/append operation 

34reqrwe
Quartz | Level 8

if you compiled the macro, then this line

%in(ABC_Place_check,113,Place_formats);  

would call the macro, substituting values of  program=ABC_Place_check , z =113 , data=Place_formats

1) define the Workshop Libname

Libname Workshop "\\Pathway\data";

2) run the sas program \\Pathway\ABC_Place_check.sas

 

%include "\\Pathway\&program..sas";

 

 3) append the datasets FMT1_ , FMT2_, .... , FMT113_ together and save to a dataset called PLACE_FORMATS in the workshop libname.

data workshop.&data;
        length LABEL $ 200;
	set %do i=1 %to &z;
	fmt&i._ %end;;
	by FMTNAME START END LABEL;
run;

Try running the statement;

options symbolgen mlogic mprint;

before you run existing macro code, then look in the log . It should help you understand what is going on . 

AMSAS
SAS Super FREQ

You have a macro call "in" and it requires 3 parameters program, z & data 

 

The following line calls the macro in and passes the values into the macro variables:

program="ABC_Time_check"

z="19"

data="Time_formats"

 

 

%in(ABC_Time_check,19,Time_formats); 

 

 

Basically where you see any &<variable name> in the macro, the &<variable name> is replaced with the value of the macro variable thus:

 

%macro in(program,z,data); 
Libname Workshop "\\Pathway\data";
%include "\\Pathway\ABC_Time_check.sas";
data workshop.Time_formats;
length LABEL $ 200;
set %do i=1 %to 19 ; fmt&i._ %end;;
by FMTNAME START END LABEL;
run;
%mend in; %in(ABC_Time_check,19,Time_formats);
%in(ABC_Place_check,113,Place_formats);

 

The %do loop is going to iterate for the values 1 to 19 and resolve to generate the following set statement 

 

set fmt1_ fmt2_ fmt3_ ... fmt19_ ;

The macro is the equivalent of the following code:

Libname Workshop "\\Pathway\data";


%include "\\Pathway\ABC_Time_check.sas";

data workshop.Time_formats;
        length LABEL $ 200;
	set fmt1_ fmt2_ fmt3_ ... fmt_19 ;
	by FMTNAME START END LABEL;
run;

 

r_behata
Barite | Level 11

Apparently the objective of code is to create SAS User Defined formats from Raw Data .

For More Information on creating the User Defined formats based on the SAS Datasets refer to:
http://support.sas.com/documentation/cdl/en/proc/65145/HTML/default/viewer.htm#n1e19y6lrektafn1kj6nb...

 

"Could someone let me know what the 'fmt&i._' "

 

&i is the counter that increments from 1 to 19 . For example when the first macro is called

%in(ABC_Time_check,19,Time_formats); 

fmt&i._ resolves to :

fmt1_
fmt2_
fmt3_
..
..
fmt19_


"refer to and the 'Z' is refer to something?  "

 

Z is the 2rd parameter to the macro, This is positional

%macro in(program,z,data);

which resolve to 19, because it is in the second place of the macro call.
%in(ABC_Time_check,19,Time_formats); .

"Also, does the 'Program' in the macro mean it need to run by using the 'data' before the do loop?"

 

every time you invoke themacro , You code is executing an external sas code before executing the data step.

 

Refer to \\Pathway\ABC_Time_check.sas and \\Pathway\ABC_Place_check.sas

 

Perhaps these sas codes create the datasets fmt1_ ..fmt19_ Etc.

 

If you want to display the contents of the External code in the current log use the following global option.

options source2;
Tom
Super User Tom
Super User

Note that SAS allows ranges of dataset names when the suffix is a number. 

So IF you changed the names of your datasets so that the number was at the end.  So FMT_1 instead of FMT1_  then you could remove the %DO loop and just use :

set fmt_1 - fmt_&z ;

If you cannot change the names but you know that you want all of the datasets that start with FMT then perhaps you could use this code:

set FMT:  ;
ybz12003
Rhodochrosite | Level 12

Thank you so much for all of elucidation.  Woman Happy

Catch up on SAS Innovate 2026

Dive into keynotes, announcements and breakthroughs on demand.

Explore 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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 2819 views
  • 1 like
  • 6 in conversation