- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hello
In this example I created a macro variable that get value Peter,Paul
My question is how to modify the code to get values "peter","Paul"
The reason is that I want to use where clause
data name_list;
length name $10;
Input name $;
datalines;
Peter
John
Paul
David
;
run;
proc sql noprint;
select name
into :names separated by ','
from name_list
where substr(name,1,1) = 'P';
quit;
%put &names;
/*Peter,Paul*/
data output;
set name_list;
where name in ("&names");
run;
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
What would you use in a data-step to add quotes to a string? Using the functions quote and trim in the select-clause solves the problem:
proc sql noprint;
select quote(trim(Name))
into :names separated by ','
from name_List
where Name like 'P%'
;
quit;
Now you need to modify the last step:
data output;
set name_list;
where name in (&names.);
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
into :names separated by '","'...where name in ("&names.");
However you really do not need to do this. Simply use a sub-query, it is far easier to program with, and expandable beyond the realms of macro language:
proc sql; create table name_list as select * from name_list where name in (select name from name_list where substr(name,1,1)="P"); quit;
In fact, (and without seeinig the rest of the process), you will most likely be better off creating flags in your data, and just putting where's in where necessary, but can't really tell as only seeing one small part.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanks
It is not working.I get error 49
proc sql noprint;
select name
into :names separated by '","'
from name_list
where substr(name,1,1) = 'P';
quit;
%put &names;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
proc sql noprint;
select quote(name)
into :names separated by ','
from name_list
where substr(name,1,1) = 'P';
quit;
But, to repeat for the x-th time, it's not necessary at all.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I do not get any error when running:
data name_list; length name $10; Input name $; datalines; Peter John Paul David ; run; proc sql noprint; select name into :names separated by '","' from name_list where substr(name,1,1) = 'P'; quit; %put &names;
Post your log in future, as text, I do note know what error 49 is or how you got it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
No need for the macro variable, it's best done in SQL with a sub-select:
proc sql;
create table output as select *
from name_list
where name in (select name from name_list where substr(name,1,1) = 'P');
quit;
Of course, since it's all from one table, it can be made still simpler:
data output;
set name_list;
where substr(name,1,1) = 'P';
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
What would you use in a data-step to add quotes to a string? Using the functions quote and trim in the select-clause solves the problem:
proc sql noprint;
select quote(trim(Name))
into :names separated by ','
from name_List
where Name like 'P%'
;
quit;
Now you need to modify the last step:
data output;
set name_list;
where name in (&names.);
run;