I have a long Proc SQl; statement in SAS which has multiple instances of this CASE statement about 50 lines long. Is it possible to store this case statement in a macro variable somehow so that it can just be referenced as &CaseStatement.? I want the user to be able to edit 1 statement that will propagate everywhere without having to edit multiple statements in the SQL code. Issue is that the case statement has line breaks and I'd like to preserve that to make it intuitive.
Example of desired functionality;
%let CaseStatement =
CASE WHEN ... THEN 1
WHEN ... THEN 6
CASE WHEN ... THEN 7 ELSE 8 END
WHEN ... THEN ... 5
END
Proc SQL;
Select
<fields>
,&CaseStatement. as CustomField
,<more fields>
FROM Table
WHERE 1 = 1
blah blah blah...
;
quit;
Please post a few complete lines of your CASE statement. There may be better ways of doing this rather than using CASE.
You might show the entire 50 line long Case statement so we can actually tell what is going on. One of the fun things with macro variables is getting stuff like quotes, parentheses and special characters to behave. So the actual code would be best determine feasibility.
Since you say " multiple instances of this CASE statement" I would ask if this is identical for multiple variables? If so then almost certainly a data step with arrays would be a better solution.
As long as the string is less than 64K bytes.
In fact your example is almost exactly how to do it. The only thing missing is you left out the semicolon to end the %LET statement.
Here is an example of a program %subnet() that actually builds the code dynamically and stores it into a macro variable
*----------------------------------------------------------------------;
* Generate query to get next unassigned node into a macro variable. ;
*----------------------------------------------------------------------;
%*----------------------------------------------------------------------
Query is modified based on type of variable used for node. This query
is put into a macro variable so it can be used twice in the program.
-----------------------------------------------------------------------;
select catx(' ','select ',case when type='num' then 'node'
else 'quote(trim(node),"''")' end
,'into :next from nodes where subnet=.')
into :getnext
from dictionary.columns
where libname='WORK' and memname='NODES' and upcase(name)='NODE'
;
so it can be used at more than one place in the program.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.