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

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);

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

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

5 REPLIES 5
Jagadishkatam
Amethyst | Level 16

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
RahulG
Barite | Level 11

 

%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,

Patrick
Opal | Level 21

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

aj34321
Quartz | Level 8

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

Reeza
Super User

Your macro variable is PLoadMethod -> missing the p. 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 5 replies
  • 5012 views
  • 4 likes
  • 5 in conversation