07-07-2017 07:10 AM
I'm looking for assistance on what should hopefully be a simple question :-)
I'm trying to build the following table, using the macro variables defined below, and the DO-loop. I'm having problems with the Target variable, as this is taking the number suffix from the loop counter...
DO i=1 TO &NUMROWS;
CellID = "&OUTTABLE1";
Target = "&OUTTABLE&i";
Any help would be greatly appreciated!
07-07-2017 07:18 AM - edited 07-07-2017 07:19 AM
Sorry, why are you using macro variables for this? Its a simple data task:
data want (drop=i); do i=1 to 4; cellid=cats("work.out",put(i,1.)); target=cellid;
07-07-2017 07:46 AM
Thanks for your reply. When this code is implemented, it won't know what values to use in the table beforehand. Even though, I've assigned simple values to the macro variables, the code will need to deal with any values that will passed to it.
07-07-2017 07:50 AM
When you have lists of values, it is best to keep them in datasets and work off those.
To know how to implement this principle, we'd have to see your code.
07-07-2017 07:19 AM
My advice is skip the macros and use a DATA step. Always remember that the macro substituions occur before the data step runs. &I (if it actually existed) would have one value. The data step variable i does exist and it has 4 values: 1 to 4.
Data Cell_Splits; DO i=1 TO 4; CellID = 'Work.Out1'; Target = cats('Woork.Data',i); Output; END; Run; proc print; run;
07-07-2017 07:20 AM - edited 07-07-2017 07:22 AM
Data Cell_Splits; DO i=1 TO &NUMROWS; CellID = "&OUTTABLE1"; Target = "&OUTTABLE&i"; Output; END; Run;
I see no place in your code where macro variable i is defined.
Data step variables cannot be used as macro variables; the macro preprocessor does its work before the data step is compiled and long before the data step runs.
Since you literally define all the macro variables anyway, why don't you use a simple datastep with datalines?
data cell_splits; length cellid target $20; input cellid target; datalines; Work.Out1 Work.Out1 Work.Out1 Work.Out2 Work.Out1 Work.Out3 Work.Out1 Work.Out4 ; run;
or even simpler
data cell_splits; retain cellid = "Work.Out1"; input target :$20.; datalines; Work.Out1 Work.Out2 Work.Out3 Work.Out4 ; run;
07-07-2017 08:12 AM
The "i" you use for looping is a column of your datase not a macrovariable so you
can not refer to it with "&i".
You can use a macro loop such as
%do i=1 %to &NUMROWS.;
and refer to your indexed macrovariables with :
Note the "&&" as &OUTABLE&i. would separately try to resolve &OUTABLE and &i. (and fail).
07-08-2017 04:01 PM - edited 07-08-2017 04:03 PM
So you have a series of macro variables and you want to create a dataset.
You can use the SYMGET() function to retreive the value of a macro variable by its name.
data want ; length i 8 CellID Target $41 ; do i=1 to &numrows ; cellid=symget(cats('outtable',i)); target=cellid; output; end; run;
Although it probably would have been better to make the dataset directly instead of first creating the series of macro variables.