Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 592
  • Last Modified:

SAS Data Sets Manipulations

How may I read and output a variable value?

For example, I would like to read the value of variable "Var09" in the very first record of the dataset "Data1" and then output that value of "Var09" in my message.

This is what I have so fa, but I am not really sure how can I make "Var09" to be equal to my counter "number" that I output in my message.

data _null_;
 set lib. Data1;
   Var09 = "number";

     file lib.messages mod;
       put "Total count is "  number;

1 Solution
d507201Database Marketing ConsultantCommented:
There are a few ways to get the number of records in a data set, which is what you seem to need.
When SAS reads data, it creates an automatic variable named _n_ that always holds the sequence number of the current record.  In a 100 record data set the first record will be 1 and the last record will be 100.  _n_ isn't added to the data set but it can be with an assignment statement like record_number=_n_;   The trick is knowing when you hit the last record.  

When reading an existing SAS data set using a SET statement there is an option named END= that you use to identify the last record.  When you use END= each record is assigned a temporary variable (not added to the data set) that has a value of 1 or 0 for yes or no answering the question 'is this the last record?'.  Together these can be used to identify the last record and then do something with it.  

data _null_;  set lib.data1 end=end_of_file;
  file out;
  if end_of_file=1 then put 'Total number of records in lib.data1 is ' _n_;

But this isn't efficient because you have to read each record to get to the last one, right?.  A much faster way is to use system functions to open the data set, get the number of records from its internal metadata, and then close the data set.  I just happen to be working on some data integration code right now in which I use this a lot for reporting where records drop out of merges.

%let _dsid=%sysfunc(open(lib.data1));
%let _number_of_records=%sysfunc(attrn(%eval(&_dsid-0),nobs));
%let _rc=%sysfunc(close(%eval(&_dsid-0)));

data _null_;  
  file out;
  put "The number of records in lib.data1 is &_number_of_records";

_dsid holds the data set identifier assigned when SAS opens the data set and _rc captures the return code from the close function.  You can name these anything and you can overwrite them each time you open and close a data set.  BE SURE to always close a data set that you open.  You could corrupt it if you don't and then you're probably screwed.

Good luck!
Aloysius LowCommented:
don't really understand what you are trying to achieve over here...

if you want to get the value in Var09 of the first record and output it, you could do something like:
data _null_;
  set lib.data1 (obs=1);
  file lib.messages mod;
  put "Value of Var09 in 1st record is: " Var09;

but then coming to your second para, you mentioned you wanted Var09 to be equal to your counter number, and your code is showing you getting the total record count - that's where the confusion comes in - what are you trying to achieve?
labradorchikAuthor Commented:
Thank you guys for helping!
lowaloysius' solution is exactly what I needed. I was just trying to output value of one variable in the body of my message.

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now