DATA Step, Macro, Functions and more

Concatenation in a data step do loop.

Reply
N/A
Posts: 0

Concatenation in a data step do loop.

I want MYSTR="X1 X2 X3 ... X500". Here X4 to X499 left out for obvious reasons.

This doesn't work:

data _null_;
length mystr $ 1892;
mystr="X1";
do i=2 to 500;
mystr=mystr || " " || "X" || strip(put(i,3.0));
end;

/* Here: Assign MYSTR to a macro variable with CALL SYMPUT for use in later data steps. */

run;

MYSTR will be "X1".

How do I solve this problem?
Valued Guide
Posts: 2,177

Re: Concatenation in a data step do loop.

Posted in reply to deleted_user
are you looking for
%let xList = %gen( prefix= X, to= 500 ) ;
where %gen is defined as the macro[pre]%macro gen( prefix=, from=1, to= 1, by=1 ) ;
%local i ;
%do i= &from %to &to %by &by;
&prefix&i
%end ;
%mend gen ; [/pre]

Why use a data step?
Super Contributor
Posts: 359

Re: Concatenation in a data step do loop.

Posted in reply to deleted_user
I think you are looking for

call symput('mystr', CATX(' ', OF X1-X500));
N/A
Posts: 0

Re: Concatenation in a data step do loop.

Peter C

Your code works. Can you explain how. Why do you get a blank between each Xn; n=1, ..., 500?

Flip

With your method I have to do like this:

data _null_ ;
array t(500) $;
do i=1 to 500;
t(i)="X" || strip(put(i,3.0));
end;
call symput('mystr', catx(' ',of t1-t500);
run;
Valued Guide
Posts: 2,177

Re: Concatenation in a data step do loop.

Posted in reply to deleted_user
sorry that I can't explain why there is no blank in [pre] %put }}%gen( prefix=ABC, to=1 ){{; [/pre] and there is that one blank in [pre] %put }}%gen( prefix=ABC, to=3 ){{;[/pre] Should you really need to eliminate those blank separators, then wrap &prefix&i with %do ; and %end ;

If for some reason, I needed to load a macro variable for multiple values of a variable in a data step, I would use call execute() inside that loop. For example a list of the boys names in the sashelp class data set to be stored in &mStudents[pre]data _null_ ;
call execute( '%nrstr( %%let mStudents = )' ) ;
do while( not EOF ) ;
set sashelp.class( where=( sex='M' )) end= EOF ;
call execute( name ) ;
end ;
call execute( ';' ) ;
stop ;
run; [/pre] The generated code should be single %let statement with a separate line for each iteration of the DO While loop.
Generally I choose the simpler solution driven normally by the context or source of the information. For collecting names into a macro variable, I would be choosing proc sql. Not sure when I would choose that data step method, but must have needed it some time.

Good luck
PeterC
N/A
Posts: 0

Re: Concatenation in a data step do loop.

The blanks should be there.

I'm just trying to understand how the code works.

In "ordinary" programming it's very common in loops with expressions like

X=X ¤ Y, where ¤ here represents some kind of function; X is updated each round of the loop.

So I would like to know how your &prefix&i(=x&i) develops in the loop to: x1, x1 x2, x1 x2 x3, ..., and so on.
SAS Super FREQ
Posts: 8,862

Re: Concatenation in a data step do loop.

Hi:
If I needed to get a list of names into a macro variable, I'd be tempted to use PROC SQL.

cynthia
[pre]
proc sql;
select distinct name into :mStudents separated by ' '
from sashelp.class
where sex = 'M'
order by name;
quit;

%put ------****** what is mStudents? &mstudents;
[/pre]
Ask a Question
Discussion stats
  • 6 replies
  • 273 views
  • 0 likes
  • 4 in conversation