Macro Parameter in SELECT CASE

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 76
Accepted Solution

Macro Parameter in SELECT CASE

Hi All,

 

Im trying to use a macro passed parameter in Proc SQL Case as below. Something is wrong in this as the output is not as expected.

Request someone to guide me if i can use this approach?

 

%macro InsertTable(pSourceTable=, pLoadMethod=, pDataTransferID=);
proc sql;
insert into <TableName>
(
Column1,
Column2,
Column3,
Column4
)
select
Column1,
Column2,
case &LoadMethod.
when 'D' then Column3
when 'F' then &pDataTransferID.
end as Column3,

case &LoadMethod.
when 'D' then Column4
when 'F' then 'I'
end as Column4
from &pSourceTable.;
quit;
%mend InsertTable;

%InsertTable(pSourceTable=SourceTable, pLoadMethod='D', pDataTransferID=1);
%InsertTable(pSourceTable=SourceTable, pLoadMethod='F', pDataTransferID=1);


Accepted Solutions
Solution
‎11-29-2016 10:29 PM
Respected Advisor
Posts: 4,137

Re: Macro Parameter in SELECT CASE

I'm not sure if it will return the intended result for you, but having a string in the CASE portion is valid syntax as such.

 

data have;
  infile datalines dsd;
  input t1 $ t2 $;
  datalines;
X,Y
X,Y
X,Y
;
run;

%let type=A;
proc sql feedback;
  create table want as
    select
    have.*,
    case "&type"
      when "A" then t1
      else t2
      end as test1,
    case "&type"
      when "B" then t1
      else t2
      end as test2
  from have
  ;
quit;

Capture.PNG

View solution in original post


All Replies
Trusted Advisor
Posts: 1,137

Re: Macro Parameter in SELECT CASE

The parameter LoadMethod should be a column name rather than value.

 

Anything after the case should be a column name and not the value.

 

could you please try replacing the 'D' with column name.

 

%macro InsertTable(pSourceTable=, pLoadMethod=, pDataTransferID=);
proc sql;
insert into <TableName>
(
Column1,
Column2,
Column3,
Column4
)
select
Column1,
Column2,
case &LoadMethod.
when 'D' then Column3
when 'F' then &pDataTransferID.
end as Column3,

case &LoadMethod.
when 'D' then Column4
when 'F' then 'I'
end as Column4
from &pSourceTable.;
quit;
%mend InsertTable;

%InsertTable(pSourceTable=SourceTable, pLoadMethod='D', pDataTransferID=1);

Thanks,
Jag
Super Contributor
Posts: 259

Re: Macro Parameter in SELECT CASE

[ Edited ]

 

%macro InsertTable(pSourceTable=, pLoadMethod=, pDataTransferID=);
proc sql;
insert into <TableName> 
(
Column1,
Column2,
Column3,
Column4
) 
select 
Column1,
Column2,
case 
when &pLoadMethod ='D' then Column3
when &pLoadMethod ='F' then &pDataTransferID.
end as Column3,
case 
when &pLoadMethod ='D' then Column4
when &pLoadMethod ='F' then 'I'
end as Column4
from &pSourceTable.;
quit;
%mend InsertTable;
%InsertTable(pSourceTable=SourceTable, pLoadMethod='D', pDataTransferID=1);

I think you are looking for the code that I have written above.

I see problem in case statement. After Case keyword there should be variable name. I do not find any macro variable as LoadMethod in the given code. Suggest you to try the code with actual variable first. Once code run fine then replace the variables with macro variable.

 

 

 

case &LoadMethod.
when 'D' then Column3
when 'F' then &pDataTransferID.
end as Column3,

Solution
‎11-29-2016 10:29 PM
Respected Advisor
Posts: 4,137

Re: Macro Parameter in SELECT CASE

I'm not sure if it will return the intended result for you, but having a string in the CASE portion is valid syntax as such.

 

data have;
  infile datalines dsd;
  input t1 $ t2 $;
  datalines;
X,Y
X,Y
X,Y
;
run;

%let type=A;
proc sql feedback;
  create table want as
    select
    have.*,
    case "&type"
      when "A" then t1
      else t2
      end as test1,
    case "&type"
      when "B" then t1
      else t2
      end as test2
  from have
  ;
quit;

Capture.PNG

Frequent Contributor
Posts: 76

Re: Macro Parameter in SELECT CASE

Thankyou guys... A great help in this case..

Super User
Posts: 19,167

Re: Macro Parameter in SELECT CASE

Your macro variable is PLoadMethod -> missing the p. 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 465 views
  • 3 likes
  • 5 in conversation