DATA Step, Macro, Functions and more

Concat list into long string

Reply
Occasional Learner
Posts: 1

Concat list into long string

[ Edited ]

I'm trying to take a long list and concatenate it into a long string/list, that I can store in a macro variable.

 

For instance:

 

I'd like to turn a dataset with a set of phone numbers like:

 

ID  Phone

1   1234567890

2   4634069845

3   9438758375

4   2349853984

5   3459086098

6   6689476988

7   3030495986

8   3459495954

 

...into a macro variable that looks like:

'1234567890', '4634069845', '9438758375', '2349853984', '3459086098', '6689476988', '3030495986', '3459495954'

 

Say we'll call it &PhoneString, then I can later create a sql query that looks like:

 

select * from applications

where phone in (&PhoneString).

 

 

Thanks!

PROC Star
Posts: 602

Re: Concat list into long string


anthdelm wrote:

I'm trying to take a long list and concatenate it into a long string/list, that I can store in a macro variable.

 

For instance:

 

I'd like to turn a dataset with a set of phone numbers like:

 

ID  Phone

1   1234567890

2   4634069845

3   9438758375

4   2349853984

5   3459086098

6   6689476988

7   3030495986

8   3459495954

 

...into a macro variable that looks like: /* look into sql into clause for creating macro vars separated by a delimiter in sas docs. the answer is in your question*/

'1234567890', '4634069845', '9438758375', '2349853984', '3459086098', '6689476988', '3030495986', '3459495954'

 

Say we'll call it &PhoneString, then I can later create a sql query that looks like:

 

select * from applications

where phone in (&PhoneString).

 

 

Thanks!


 

Super User
Posts: 3,371

Re: Concat list into long string

proc sql noprint;
  select quote(PhoneNumber)
  into :PhoneList separated by ","
  from PhoneList;
quit;

Note the macro variable PhoneList is limited to 32K characters in length.

Super User
Posts: 8,315

Re: Concat list into long string

[ Edited ]
data cntlin;
set phone_numbers (keep=phone rename=(phone=start)) end=done;
type = 'C';
fmtname = "select_phone";
length label $10;
label = 'yes';
output;
if done
then do;
  label = 'no';
  start = '**OTHER**';
  hlo = 'O';
  output;
end;
run;

proc sql;
select * from applications where put(phone,$select_phone.) = 'yes';
quit;

Using a format avoids problems with macro variable overflows, and creates short code even with options mprint active. Your only limit is your available memory for loading the format. And that is much higher than the 32767 of a macro variable.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
PROC Star
Posts: 955

Re: Concat list into long string

data have;
input ID Phone$;
datalines;
1 1234567890
2 4634069845
3 9438758375
4 2349853984
5 3459086098
6 6689476988
7 3030495986
8 3459495954
;

proc sql noprint;
	select quote(Phone,"'") into :PhoneNums separated by ',' from have;
run;

%put &PhoneNums.;
Highlighted
Super Contributor
Posts: 370

Re: Concat list into long string

Would change

select * from applications
    where phone in (&PhoneString)

into

select * from applications
    where phone in (select phone from phoneNumbers)
Super User
Posts: 8,315

Re: Concat list into long string

Posted in reply to andreas_lds

Another nice solution that avoids creating an unwieldy macro variable.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Ask a Question
Discussion stats
  • 6 replies
  • 300 views
  • 7 likes
  • 6 in conversation