Desktop productivity for business analysts and programmers

Question on indirect variable references

New Contributor
Posts: 2

Question on indirect variable references

I have a series of configuration files with parameter in the format:
where the paramter name is on the left of the colon and the parameter value is to the right of the colon.
Number of parameters per device varies and not all parameters are in the file for all devices.
I was thinking an indirect reference that used a variable that has the value of the variable would work great but it does not appear to be a feature of base sas (v 9.1.3 on zOS). I am missing something in the doc?
Any thoughts on the best way to proceed?
Posts: 9,431

Re: Question on indirect variable references

There is a way to perform indirect referencing with SAS macro variables. The issue, in your situation, is how to get your parameter information into macro variables. There are several different ways to do this and your best bet for help is to contact SAS Technical Support.

Here's an example of indirect referencing of macro variables that are assigned with a %LET statement. You can enter this code into an EG code node, execute the code and then review the results in the SAS log for the code node.

1) create some macro variables
%let muppet1 = kermit;
%let muppet2 = gonzo;
%let sesame1 = big bird;
%let sesame2 = elmo;

%put --> muppet1= &muppet1;
%put --> muppet2= &muppet2;
%put --> sesame1= &sesame1;
%put --> sesame2= &sesame2;

Here in the above example, I'm creating some macro variables for various puppet characters from the Muppet Show and Sesame Street. The %PUTs will write the values of the macro variables &MUPPET1, &MUPPET2, &SESAME1 and &SESAME2 to the SAS Log.
38 %put --> muppet1= &muppet1;
--> muppet1= kermit
39 %put --> muppet2= &muppet2;
--> muppet2= gonzo
40 %put --> sesame1= &sesame1;
--> sesame1= big bird
41 %put --> sesame2= &sesame2;
--> sesame2= elmo

However, what if I want to be able to pick the show and pick the character number using another set of macro variables &SHOW could either be MUPPET or SESAME and &CHAR could be either 1 or 2:
%let show = muppet;
%let char = 1;

%put -->I want show &show and character number &char used together = &&&show&char;

%let show = sesame;
%let char = 2;

%put -->I want show &show and character number &char used together = &&&show&char;

In the above instance, the &&&SHOW&CHAR is the way that you would use INDIRECT referencing of macro variables in a very simple example. The 3 ampersands in front of SHOW essentially delay the resolution of the macro variable until &CHAR has been resolved, so think of it happening this way:
1) &&&SHOW&CHAR resolves to &&MUPPET&CHAR
2) &&MUPPET&CHAR resolves to &MUPPET1
3) &MUPPET1 resolves to kermit
And the results in the log are:
63 %let show = muppet;
64 %let char = 1;
66 %put -->I want show &show and character number &char used together = &&&show&char;
-->I want show muppet and character number 1 used together = kermit
68 %let show = sesame;
69 %let char = 2;
71 %put -->I want show &show and character number &char used together = &&&show&char;
-->I want show sesame and character number 2 used together = elmo

Of course, there's a lot more involved than just using %LET and %PUT to implement what you want to do. That's where looking in the SAS Macro Facility reference and contacting Tech Support will help you come up with the best solution for your particular challenge. There are a lot of SAS Macro functions (%SUBSTR, %SCAN) that allow you to manipulate strings and break them into component parts, in addition to the %SYSFUNC function that allows you to use DATA step functions if there aren't macro functions to do what you want.

I'm not entirely clear on how your parameters get set, or where in the entire process SAS and EG come into the picture, so I really can't make any other recommendations. But this is exactly the kind of thing that Tech Support can help you with, particularly if the parameters you're talking about are operating system or device specific.

Not applicable
Posts: 0

Re: Question on indirect variable references

It is not clear what you are wanting to do.

to parse the input, use the scan function

parameter = scan(argument,1,':');
value = scan(argument,2,':');

If you now want to pull/use the parameter value within a SAS program, that may be more difficult, depending on what you are doing or want to do.

One way is to create a macro variable of the same name as the parameter and give it the value:

data _null_;
infile configurations scanover;
input argument $ @;
parameter = scan ...
value = scan ...
call symput(parameter,value);

this code is not perfect and will need to be debugged, etc. but it shows the general idea. A parameter:value pair is read in and parsed. Then the "call symput" function creates a macro variable with the same name as the parameter and the same value as the parameter's value.

Let's say that you have the parameter pair:


Then the code would create a macro variable named "DUMMY" and give it the value of "yes". To use DUMMY you would then have in your code the macro variable &dummy -- e.g. If "&dummy" = yes then do; ... ; end;

hope this helps.
New Contributor
Posts: 2

Re: Question on indirect variable references

Cynthia and Chuck, thanks for the quick replies. I had been playing with the macros and the symput symget functions and I was having trouble getting exactly what I wanted. i will have to continue to play with this.

For the short term I am using arrays to handle the parameter names and values.
Not applicable
Posts: 0

Re: Question on indirect variable references

Common problems with the use of symput and symget is the timing of when things occur -- data step processing vs. macro processing.

I have found in my own experience with trying to use symget is that I mis-understood what a macro is and how it is meant to be used. Especially within the confines of a single data/run; block.

Perhaps if you could share a little more detail of what you are trying to do, we may be able to offer more/better help. A code sample would be good.

Perhaps what you are trying to do may be better served through a data conversion effected via a format? As an example, we have a "holiday." format that converts a date value into a 1 or 0, depending on whether the date is a holiday or not. This is accomplished by holiday = put(date,holiday.); if not holiday then do; ... that is, if not zero then do .... the holiday format has the explicit dates of our business's holidays for a few years in advance.
Ask a Question
Discussion stats
  • 4 replies
  • 3 in conversation