Hi,
I am using the following code to dynamically create formats based on a SAS work dataset.
data _null_;
set cd_10_&ODS_dsn end=eof;
if _n_ = 1 then do;
put @1 "proc format;";
put @8 "value $&ODS_dsn";
end;
put @8 VariableOID @20 "= ' "cdval"'";
if eof then do;
put @8 ";";
put @1 "run;";
end;
run;
The code works fine. For example, here is result in the log file.
proc format;
value $CMCDB
CMINDTYP = ' #Coded_Terms!A244 '
CMONGO = ' #Coded_Terms!A247 '
;
run;
NOTE: There were 2 observations read from the data set WORK.CD_10_CMCDB.
What I do not see in the log file is a message that the format $CMCDB. has been outputted. When I try to use this format later in the program, the format is not found.
Hi @cgsmoak56,
@cgsmoak56 wrote:
What I do not see in the log file is a message that the format $CMCDB. has been outputted. When I try to use this format later in the program, the format is not found.
This is because you wrote the code to the log (as text), but did not execute it. This can be fixed easily: Write the code to a (e.g., temporary) text file and then submit the content of that file via %INCLUDE.
Only three statements need to be inserted into your code:
filename fmtcode temp; data _null_; file fmtcode; <your code> run; %include fmtcode / source;
Thank you. That should also work.
Make sure you show us the code you actually use. Best is to copy from the log, the entire proc format and any notes, the paste into a text box opened on the forum with the </> icon at the top of the message window.
The CNTLIN= option uses a standardized data set in Proc Format to create formats.
Your code would have to 1) write the code to a text file and then 2) use %include to make that execute.
data _null_; set cd_10_&ODS_dsn end=eof; file "<path to file>\makemyformat.sas"; if _n_ = 1 then do; put @1 "proc format;"; put @8 "value $&ODS_dsn"; end; put @8 VariableOID @20 "= ' "cdval"'"; if eof then do; put @8 ";"; put @1 "run;"; end; run; %include "<path to file>\makemyformat.sas";
or use "call execute" instead of "put" to place the generated statements into the execution stack.
Just "putting" text to the log does not write any actual code that is executed.
Hi @cgsmoak56,
@cgsmoak56 wrote:
What I do not see in the log file is a message that the format $CMCDB. has been outputted. When I try to use this format later in the program, the format is not found.
This is because you wrote the code to the log (as text), but did not execute it. This can be fixed easily: Write the code to a (e.g., temporary) text file and then submit the content of that file via %INCLUDE.
Only three statements need to be inserted into your code:
filename fmtcode temp; data _null_; file fmtcode; <your code> run; %include fmtcode / source;
Thank you. This solution worked!
Hello,
If you provide your input data set WORK.CD_10_CMCDB (2 records) in a data step with datalines (cards;), I will have a look at your data-driven code generation.
But in general it's better to use the CNTLIN= option to generate a format from a data set.
See here:
Base SAS Procedures Guide -- FORMAT Procedure
Example 13: Creating a Format from a CNTLIN= Data Set
https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/proc/n1e19y6lrektafn1kj6nbvhus59w.htm
Good luck,
Koen
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!
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.
Ready to level-up your skills? Choose your own adventure.