Solved

SAS Data Sets Manipulations

Posted on 2011-03-24
3
553 Views
Last Modified: 2013-11-16
Hello,
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;
run;


 
 
0
Comment
Question by:labradorchik
3 Comments
 
LVL 7

Expert Comment

by:d507201
Comment Utility
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_;
run;

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";
run;

_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!
0
 
LVL 14

Accepted Solution

by:
Aloysius Low earned 500 total points
Comment Utility
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;
run;

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?
0
 

Author Closing Comment

by:labradorchik
Comment Utility
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.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

I guess that all of us know that caching the data usually increase the performance, but I worried if all of us are aware about the risk that caching the data provides and how to minimize this.  That’s the reason why I decided to write this short art…
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now