BookmarkSubscribeRSS Feed
Rajeev8080
Calcite | Level 5
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;
3 REPLIES 3
mohamed_zaki
Barite | Level 11

@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.

 

 

Rajeev8080
Calcite | Level 5

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.

Reeza
Super User

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

 

 

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
  • 3 replies
  • 1039 views
  • 2 likes
  • 3 in conversation