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);
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;
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);
%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,
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;
Thankyou guys... A great help in this case..
Your macro variable is PLoadMethod -> missing the p.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.