@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.
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.
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
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.