Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SAS Data Sets Manipulations

Posted on 2011-03-24
3
Medium Priority
?
586 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 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
Lotus Notes has been used since a very long time as an e-mail client and is very popular because of it's unmatched security. In this article we are going to learn about  RRV Bucket corruption and understand various methods to Fix "RRV Bucket Corrupt…
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

719 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