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.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.