Solved

SAS Data Sets Manipulations

Posted on 2011-03-24
3
577 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 7

Expert Comment

by:d507201
ID: 35211240
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
ID: 35212205
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
ID: 35235583
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

Get Database Help Now w/ Support & Database Audit

Keeping your database environment tuned, optimized and high-performance is key to achieving business goals. If your database goes down, so does your business. Percona experts have a long history of helping enterprises ensure their databases are running smoothly.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
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…

752 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