BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
rramcharan
Fluorite | Level 6

Hi I have a variable being passed from excel which is

 

x=CIRT201808, CIRT201608, 18DI01;

 

I want to surround each value with a single quote then use a macro to say

 

if z in ('CIRT201808', 'CIRT201608', '18DI01') then do something.

 

Any ideas?

 

Cheers

 

Rob

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

This works on a string that has the variables separated by spaces and no commas (which you can create using the TRANSLATE function if your original text string has the commas). Alternatively, the macro can be modified to handle the commas, but I didn't do that.

 

%macro put_quotes_around(string);
	%let aggregate=;
	%do i=1 %to %sysfunc(countw(&string));
		%let thisword=%scan(&string,&i,%str( ));
		%let thiswordq=%nrstr(%')&thisword%nrstr(%');
		%if &i < %sysfunc(countw(&string)) %then %let comma=%str(,); %else %let comma=;
		%let aggregate=&aggregate &thiswordq &comma;
	%end;
	%put &=aggregate;
%mend;
%put_quotes_around(CIRT201808 CIRT201608 18DI01)

 

--
Paige Miller

View solution in original post

9 REPLIES 9
PaigeMiller
Diamond | Level 26

This works on a string that has the variables separated by spaces and no commas (which you can create using the TRANSLATE function if your original text string has the commas). Alternatively, the macro can be modified to handle the commas, but I didn't do that.

 

%macro put_quotes_around(string);
	%let aggregate=;
	%do i=1 %to %sysfunc(countw(&string));
		%let thisword=%scan(&string,&i,%str( ));
		%let thiswordq=%nrstr(%')&thisword%nrstr(%');
		%if &i < %sysfunc(countw(&string)) %then %let comma=%str(,); %else %let comma=;
		%let aggregate=&aggregate &thiswordq &comma;
	%end;
	%put &=aggregate;
%mend;
%put_quotes_around(CIRT201808 CIRT201608 18DI01)

 

--
Paige Miller
rramcharan
Fluorite | Level 6

excellent thanks!

PaigeMiller
Diamond | Level 26

I should add, for completeness, to use this macro for example, in PROC SQL, comment out the %PUT statement near the end, then add a line that just says &aggregate as the last line of the macro just above the %mend; command, then

 

proc sql;
      something something something ...
      where variable in (%put_quotes_around(CIRT2018080 CIRT201608 18DI01));
quit;

 

--
Paige Miller
RW9
Diamond | Level 26 RW9
Diamond | Level 26

How do you "get a variable passed from Excel"?  If you have data, then store it in a dataset, then use the dataset, it is far simpler, easier coding, and more robust to do:

proc sql;
  create table want as
  select * 
  from   have
  where z in (select param from paramdataset);
quit;

So paramdataset would look like:
Param

CIRT201808

CIRT201608

...

 

The above dataset is expandable far beyond the range of macro variables, and is far simpler to work with.

PaigeMiller
Diamond | Level 26

Suppose he gets a string from Excel. The string is: CIRT201808, CIRT201608, 18DI01 (with or without the commas)

 

I realize it's not an ideal way to do things, but sometimes you don't get to choose the format of the data because others are creating the Excel file and you have no control over it.

--
Paige Miller
RW9
Diamond | Level 26 RW9
Diamond | Level 26
data want (keep=word);
  do i=1 to countw(string_from_excel," ");
    word=scan(string_from_excel,i," ");
    output;
  end;
run;

Whilst not knowing how the string is got, i.e. if its an import just read it into a dataste there, its pretty simple to break it out into data, this may however be overkill for 1 or 2 parameters, but in that case just change the source.

I agree of course that sometimes there are restrictions, but how many times are those restrictions self imposed?

PaigeMiller
Diamond | Level 26

@RW9 wrote:
data want (keep=word);
  do i=1 to countw(string_from_excel," ");
    word=scan(string_from_excel,i," ");
    output;
  end;
run;

Whilst not knowing how the string is got, i.e. if its an import just read it into a dataste there, its pretty simple to break it out into data, this may however be overkill for 1 or 2 parameters, but in that case just change the source.

I agree of course that sometimes there are restrictions, but how many times are those restrictions self imposed?


Yes you could type this code every time you encounter this situation to get the string values into a data step; or you could simply, one time, create a macro to do get the end result you want (as I have done) and avoid the data step portion, and then use %put_quotes_around(&macrovariablename) ... which is easier to remember, and in my opinion, more readable.

 

By the way, yes this is data, it is string data, so data steps will do a fine job on it; and macros work well on string data in this case.

--
Paige Miller
Ksharp
Super User

if z in ('CIRT201808', 'CIRT201608', '18DI01') then do something.

 

-->

 

if find(symget('x'),strip(z)) then do something.

Ksharp
Super User

if z in ('CIRT201808', 'CIRT201608', '18DI01') then do something.

 

-->

 

if find(symget('x'),strip(z)) then do something.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 1628 views
  • 2 likes
  • 4 in conversation