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

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

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;

 

 

View solution in original post

7 REPLIES 7
Reeza
Super User
Are you familiar with a CNTLIN data set? SAS can create a format from a data set directly.

cgsmoak56
Calcite | Level 5

Thank you.  That should also work.

Reeza
Super User
And you need CALL EXECUTE() not PUT.
ballardw
Super User

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.

FreelanceReinh
Jade | Level 19

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;

 

 

cgsmoak56
Calcite | Level 5

Thank you.  This solution worked!

sbxkoenk
SAS Super FREQ

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

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 7 replies
  • 765 views
  • 0 likes
  • 5 in conversation