BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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?
6 REPLIES 6
Peter_C
Rhodochrosite | Level 12
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?
Flip
Fluorite | Level 6
I think you are looking for

call symput('mystr', CATX(' ', OF X1-X500));
deleted_user
Not applicable
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;
Peter_C
Rhodochrosite | Level 12
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
deleted_user
Not applicable
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.
Cynthia_sas
SAS Super FREQ
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]

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 6 replies
  • 2207 views
  • 0 likes
  • 4 in conversation