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

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?

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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 

View solution in original post

7 REPLIES 7
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

brink
Calcite | Level 5

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Patrick
Opal | Level 21

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

Astounding
PROC Star

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;

Kurt_Bremser
Super User

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 
brink
Calcite | Level 5

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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 7 replies
  • 902 views
  • 0 likes
  • 5 in conversation