DATA Step, Macro, Functions and more

macro to quote variables.

Reply
Regular Contributor
Posts: 190

macro to quote variables.

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 

Trusted Advisor
Posts: 1,137

Re: macro to quote variables.

[ Edited ]

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
Super User
Super User
Posts: 7,942

Re: macro to quote variables.

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.  

 

Regular Contributor
Posts: 190

Re: macro to quote variables.

@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??
Super User
Super User
Posts: 7,942

Re: macro to quote variables.

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?

Regular Contributor
Posts: 190

Re: macro to quote variables.

nope am understanding macros as am working on it....
Super User
Super User
Posts: 7,942

Re: macro to quote variables.

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.

Regular Contributor
Posts: 190

Re: macro to quote variables.

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 ....
Trusted Advisor
Posts: 1,913

Re: macro to quote variables.

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;
Super User
Posts: 11,343

Re: macro to quote variables.

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

Ask a Question
Discussion stats
  • 9 replies
  • 339 views
  • 0 likes
  • 5 in conversation