BookmarkSubscribeRSS Feed
HoaTruong
Obsidian | Level 7

Hi,

 

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

 

 

iCellIDTarget
1Work.Out1Work.Out1
2Work.Out1Work.Out2
3Work.Out1Work.Out3
4Work.Out1Work.Out4

 

 

%LET NUMROWS=4;

%LET OUTTABLE1=Work.Out1;

%LET OUTTABLE2=Work.Out2;

%LET OUTTABLE3=Work.Out3;

%LET OUTTABLE4=Work.Out4;

 

 

Data Cell_Splits;

DO i=1 TO &NUMROWS;

CellID = "&OUTTABLE1";

Target = "&OUTTABLE&i";

Output;

END;

Run;

 

Any help would be greatly appreciated!

 

Many thanks

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

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;
end; run;
HoaTruong
Obsidian | Level 7

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.

Kurt_Bremser
Super User

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.

WarrenKuhfeld
Rhodochrosite | Level 12

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;
Kurt_Bremser
Super User
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;
gamotte
Rhodochrosite | Level 12

Hello,

 

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

...

%end;

 

and refer to your indexed macrovariables with :

&&OUTTABLE&i.

 

Note the "&&" as &OUTABLE&i. would separately try to resolve &OUTABLE and &i. (and fail).

 

Tom
Super User Tom
Super User

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. 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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

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
  • 871 views
  • 0 likes
  • 6 in conversation