How do I feed data step loop iterator value to macro

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

How do I feed data step loop iterator value to macro

Hello,

 

I am somewhat new to SAS programming and pretty confused about SAS macros. I hope you can help me out. I am trying to create a dataset containing locations for different files. In order to do that I have a dataset loop which executes a macro that assigns the path.

 

%macro chosepath(i);
%global path;
%if &i = 1 %then %let path = path_1;
%if &i = 2 %then %let path = path_2;
%else %let path = nopath;
%mend chosepath;

data dataset;
do i = 1 to 2;
%chosepath(i);
pathvar = "&path";
output;
end;
run;

The problems is that the %if statements are never true. In stead of feeding the iterator (1, 2 ..) to the macro it always gets the iterator name (i). Is there any way for the macro to get the actual value?

 


Accepted Solutions
Solution
‎09-18-2017 04:22 AM
Super User
Posts: 8,589

Re: How do I feed data step loop iterator value to macro

If you want to feed a list of data into your data step, you can do this:

%let pathvar=path_1,path_2;

data dataset;
do i = 1 to 2;
  pathvar = scan("&pathvar",i,',');
  output;
end;
run;

proc print data=dataset noobs;
run;

Result:

i    pathvar

1    path_1 
2    path_2 
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Super User
Super User
Posts: 8,634

Re: How do I feed data step loop iterator value to macro

Yes, classic misunderstanding of what Macro is and how it relates to Base SAS.

Base SAS is the programming language.  Macro SAS is a text generator which then feeds into Base SAS.  So you %if statement resolves before the datastep ever starts execution.  You have to think of it this way:

Text -> Macro Preprocessort -> full Code file -> SAS Compliler

In that order sequentially, so all macro is resolved before getting anywhere near compilation.

 

I would ask why you want to do this at all as the code makes no sense, as with 99.9% of all macro code I have seen it is just adding a layer of obfuscation for no benefit, just do:

data dataset;
  do i=1 to 2;
    pathvar=cats("path_",put(i,1.));
  end;
run;

Note that calling your dataset "dataset is not recommended.

New Contributor
Posts: 3

Re: How do I feed data step loop iterator value to macro

Thank you for your reply.

 

The paths are unfortunately not sqeuential but could be path_1 = C:\somefolder\anotherfolder, path_2 = F:\entirelydifferentfolder ..

 

If I cange the line to

 

%chosepath(2);

 

then the %if statement resolves correctly (but of course only gives me path 2).

Super User
Super User
Posts: 8,634

Re: How do I feed data step loop iterator value to macro

Please supply something which illustrates your problem then, I can only work with what you provide.  Irrespective of what the logic is, macro is not the way forward.  If you have a few paths then put them in a dataset, it then becomes a simple matter to merge them on:

data paths;
  no=1;  path="c:\something"; output;
  no=2; path="f:\somethingelse"; output;
run;

data want;
  merge have paths;
  by no;
run;

Path will then be merged on where they no matches.

Respected Advisor
Posts: 4,274

Re: How do I feed data step loop iterator value to macro

[ Edited ]

@brink

May be explain us the bigger problem you want to solve, i.e. is this about reading same structured text files from multiple locations into a common SAS dataset?

It also really helps if you can provide sample data or at least explain as detailed as possible what you have, what problem you want to solve and what detail you need help with.

It also helps if you can provide the code you've already implemented (whether working or not - you've done this already) as this often also helps us to understand where you're coming from and it also shows us your level of SAS expertise so we can give you answers on the right level.

Super User
Posts: 5,972

Re: How do I feed data step loop iterator value to macro

There's no source of data for this DATA step.  Are you expecting that PATH_1 and PATH_2 are actually macro variables?  If so, you could simply code:

 

data dataset;

length path $ 200;

pathvar = "&path_1";

output;

pathvar = "&path_2";

output;

run;

Solution
‎09-18-2017 04:22 AM
Super User
Posts: 8,589

Re: How do I feed data step loop iterator value to macro

If you want to feed a list of data into your data step, you can do this:

%let pathvar=path_1,path_2;

data dataset;
do i = 1 to 2;
  pathvar = scan("&pathvar",i,',');
  output;
end;
run;

proc print data=dataset noobs;
run;

Result:

i    pathvar

1    path_1 
2    path_2 
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
New Contributor
Posts: 3

Re: How do I feed data step loop iterator value to macro

Posted in reply to KurtBremser

Perfect. This is exactly what I needed. Thank you.

 

I wanted my variables to stay as macro variables for as longs as possible to avoid truncation problems when concatenatiing various parts of a path, file name, version and file type. The total path should only be saved in the dataset when the combinations are done.

☑ This topic is solved.

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

Discussion stats
  • 7 replies
  • 208 views
  • 0 likes
  • 5 in conversation