DATA Step, Macro, Functions and more

Macro with single quotes

Reply
Frequent Contributor
Posts: 85

Macro with single quotes

Hi there,

My task is to create individual datasets from the variable of my dimension. However I am facing issue with values which has single quotes... Please find the code below

Data Dimension;

input Name $40.;

datalines;

property

worker's compensation

calamity

Life's data

;

run;

Proc sql;

select distinct Name into : nm separated by '*' from dimension;

select count distinct(Name) into :count from dimension;

QUIT;

%Macro m1;

%do i=1 %to count1;

%let nm1=%scan(%superquote(nm),&i,'*');

data new&i;

set Fact;

where Fact_name="&nm1";

run;

%end;

%mend;

%m1;

Here my nm1 resolves for property but not for worker's compensation... has anybody encountered any problem like this before... any help is really appreciated...

Super User
Posts: 10,046

Macro with single quotes

Posted in reply to forumsguy

I found several problems in your code.

data fact;
input fact_Name $40.;
datalines;
property
worker's compensation
calamity
Life's data
;
run;



Data Dimension;
input Name $40.;
datalines;
property
worker's compensation
calamity
Life's data
;
run;

 

Proc sql;
select distinct Name into : nm separated by '*' from dimension;
select count (distinct Name) into :count from dimension;
QUIT;


%Macro m1;
%do i=1 %to &count;
%let nm1=%qscan(%bquote(&nm),&i,*); 
data new&i;
set Fact;
where Fact_name="&nm1";
run;
%end;
%mend;

 

%m1

Ksharp

Super User
Posts: 10,046

Macro with single quotes

Posted in reply to forumsguy

It is a simpler solution.

data fact;
input fact_Name $40.;
datalines;
property
worker's compensation
calamity
Life's data
;
run;



Data Dimension;
input Name $40.;
datalines;
property
worker's compensation
calamity
Life's data
;
run;

data _null_;
 set dimension;
 call execute(cats('data new',_n_,'; set fact; where fact_name="',name,'";run;'));
run;


Ksharp

Super User
Super User
Posts: 7,080

Re: Macro with single quotes

Posted in reply to forumsguy

The QUOTE() function helps eliminate the need for macro quoting in situations like this.

proc sql noprint ;

  select distinct quote(trim(name)) into : nm separated by '*' from dimension;

%let count=&sqlobs;

quit;

%do i=1 %to &count;

data new&i;

  set Fact;

  where Fact_name=%scan(&nm,&i,*);

run;

%end;

Super User
Super User
Posts: 7,080

Re: Macro with single quotes

If you need to eliminate some I/O then you can actually create all of the datasets in a single pass.

data %do i=1 %to &count; new&i %end; ;

  set Fact;

%do i=1 %to &count;

  if Fact_name=%scan(&nm,&i,*) then output new&i;

%end;

run;

Super Contributor
Posts: 1,636

Re: Macro with single quotes

Hi Tom,

why "if Fact_name=%scan(&nm,&i,*) then output new&i;"

not

"if Fact_name=%scan(&nm,&i,"*") then output new&i;" (it doesn't work) ?

Thank you!  - Linlin

Super User
Super User
Posts: 7,080

Re: Macro with single quotes

In macro code everything is a string. So you do not need to quotes things. And if you do the quotes become part of the string.

In this case the it will break if you include the quotes in the delimiter list for the %SCAN() function. Instead of generating

  fact_name="Life's data"

it would generate

   fact_name=Life's data

Which would both have unbalanced single quotes and be invalid SAS syntax.

Super Contributor
Posts: 1,636

Re: Macro with single quotes

Thank you Tom!  I will try to remember that.   - Linlin

Ask a Question
Discussion stats
  • 7 replies
  • 691 views
  • 0 likes
  • 4 in conversation