BookmarkSubscribeRSS Feed
RTelang
Fluorite | Level 6

A macro that adds quotes around each item in a list. This list can consist of individual items or a previously defined macro variable containing values. thanks in advance 

9 REPLIES 9
Jagadishkatam
Amethyst | Level 16

May be you will like to try something as below

 

I used the quote function to quote the individual values and used strip to remove the trailing spaces.

proc sql;
select distinct strip(quote(name)) into :names separated by ' ' from sashelp.class;
quit;

%put &names;

Thanks,
Jag
RW9
Diamond | Level 26 RW9
Diamond | Level 26

For what purpose, and what type of data?  If you want to do sql in's then use a table:

proc sql;
  create table WANT as
  select  *
  from    HAVE
  where AVARIABLE in (select CODE from CODELIST);
quit;

If you need a list of data for any purpose a dataset is a far better storage unit for it.  Even if somewhere down the line you really have to do it witha  macro list (and I have never come across a scenario where there isn't a better method) then you would just use sql select into separated by.  However bear in mind the limitations with such a process, and error checking - maybe the data contains quotes/specials etc.  

 

RTelang
Fluorite | Level 6
@RW9 @jagdish can u provide a specific generic macro code that adds quotes around each item in a list. This list can consist of individual items or a previously defined macro variable containing values??
RW9
Diamond | Level 26 RW9
Diamond | Level 26

I am beginning to suspect with your questions that you want the community to write an entire macro library based off some idea you have to do everything in macro code, is this the case?

RTelang
Fluorite | Level 6
nope am understanding macros as am working on it....
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Could I suggest then that you first start by learning Base SAS:

http://support.sas.com/training/tutorial/

Is a good start, then maybe some other courses.  Understanding of "where" macro language should be used is as important as "how" it should be used.  As mentioned, in all of your posts so far, it has not been so much of a question / answer request but more of a please provide code to do XYZ.  Understanding of how to do things in Base SAS - which is the underlying programming language will solve most, if not all of the tasks you are trying to do.  Once you have that down, you can then start to tlook at what conponents could be more useful as macros, and what systems require.  Follow Software Development Lifecycle process also, develop a document which details the full process, then break that process out into separate processes, create functional design specifications for each process which would detail data flow, checks which need to be performed etc.  Once you have docuemented the entire thing, then it should be a simple matter to write some code to action those documented processes.  Forcing code into macro, especially if you do not understand it, and have no clear concept to follow is a recipe for disaster.  

Rule no 1 for coding:

Plan, document, implement, test.  

 

With regards to providing code, consider hiring a contractor to create code based on your FDS if you need to.

RTelang
Fluorite | Level 6
no no u get me wrong @RW9 i know what am i doing i know macros am jst asking suggestions for my doubts & i send this doubt here for some insight....@rw9 so ....
PaigeMiller
Diamond | Level 26

UNTESTED CODE

 

%macro put_quotes_around(string=);
	%local ii nwords newstring;
	%let nwords=%sysfunc(countw(&string));
	%let newstring=;
	%do ii=1 %to &nwords;
		%let thisword=%scan(&string,&ii,%str( ));
		%let newstring=&newstring %str(%')&thisword%str(%');
	%end;
	%unquote(&newstring)
%mend;
%let newmacrovar=%put_quotes_around(string=ABC DEF GHI);
%put NEWMACROVAR &newmacrovar;
--
Paige Miller
ballardw
Super User

How are your values delimited? Are there spaces within the values you want quoted?

 

A one minute search on Google for Sas macro variable lists would give you many examples starting with http://www2.sas.com/proceedings/forum2007/113-2007.pdf

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
  • 9 replies
  • 1161 views
  • 0 likes
  • 5 in conversation