DATA Step, Macro, Functions and more

PROC dynamic format ..how if _n_ works?

Reply
Occasional Contributor
Posts: 8

PROC dynamic format ..how if _n_ works?

The below is an example taken from google. how if _n_ works here. if i remind myself for PDV it should return only  1 entry as per condition _n_=1, how it returns all of below acctinfo values?
 
 
 
Approach 2 – Writing SAS code that is later used in the program
 
This approach employs PUT statement to generate SAS code in an external file and then uses %INCLUDE to bring
the generated code into the program. In the following example, an existing SAS data set (ACCTINFO) contains
account codes and corresponding customer names. There is one observation per account number. The observations
 
in the data set are as follows:
 
 ACCTNUM NAME
 5008074 John Smith
5008075 Bill Jones
 5009766 Benjamin Estes
.
.
.
Using the data in this data set, we want to create a format that converts an account code into a customer name. The
code can be set up as the following:
filename tmpfmt ‘external_file’;
data _null_;
 set acctinfo end=eof;
 file tmpfmt;
 if _n_=1 then do;
 put @1 "proc format;" ;
put @8 "value account";
 end;
 put @8 acctnum @20 "= '" name "'";
 
 if eof then do;
 put @8 ";";
 put @1 "run;";
 end;
run;
%include tmpfmt;
The dynamically created code produces:
proc format;
 value account
 5008074 = 'John Smith '
 5008075 = 'Bill Jones '
 5009766 = 'Benjamin Estes '
 .
 .
 .
 ;
 
 run;
Super Contributor
Posts: 490

Re: PROC dynamic format ..how if _n_ works?

[ Edited ]
Posted in reply to Rajeev8080

Rajeev8080 wrote:
The below is an example taken from google. how if _n_ works here. if i remind myself for PDV it should return only  1 entry as per condition _n_=1, how it returns all of below acctinfo values?
 

 

The data step have three main part

 

if _n_=1 then do;
 put @1 "proc format;" ;
put @8 "value account";
 end;

 

This part _surronded by DO and END_ will actually run one time, when reading the first observation from acctinfo dataset. Where the condition IF (_n_= 1) is true. 

 

The second part of the code is the one that returns all of the acctinfo values, which run without any condition in each data step iteration.

 

 put @8 acctnum @20 "= '" name "'";

 

Finally the part that run only when reading the last observation, where the condition IF (eof = true) is valid and that will happen only one time with you datset.

 

 if eof then do;
 put @8 ";";
 put @1 "run;";
 end;

 

 

 So the code within the first condition if _n_=1  is not the part that returns all the acctinfo values. But it will be valid for one time based on the condition and the two PUT statment will write to the file only one time.

 

 

Occasional Contributor
Posts: 8

Re: PROC dynamic format ..how if _n_ works?

Posted in reply to mohamed_zaki

Ideally it should return 1 if we think of PDV . however, it returns many values with this condition while use in the PROC format that i like to use for further use.

Super User
Posts: 19,789

Re: PROC dynamic format ..how if _n_ works?

Posted in reply to Rajeev8080

It's great to see that you searched and found a solution that you're trying to understsand.

 

I would like to mention that is not a good way to create a dynamic format. 

You can use the CNTLIN to create a dataset without creating a new file and reading/executing it. 

 

Example 8 in this paper demonstrates the method:

http://www2.sas.com/proceedings/sugi30/001-30.pdf

 

Here's a paper dedicated to that method alone:

http://www2.sas.com/proceedings/forum2007/068-2007.pdf

 

 

Ask a Question
Discussion stats
  • 3 replies
  • 332 views
  • 2 likes
  • 3 in conversation