View previous topic :: View next topic
|
Author |
Message |
ppandey07
New User
Joined: 27 Nov 2008 Posts: 51 Location: Delhi, India
|
|
|
|
Hi,
I am using SAS for fetching data from Database.
I have fetched the data in das data. Now I want to format that data into flat file. In flat file I also need one header record.
So my questions are.
1)How can I create header record while copying data from SAS data set to flat file.
2) How can I count number of records copied and put in header record.
Thank you!!! |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8696 Location: Dubuque, Iowa, USA
|
|
|
|
Run a data step (HDRCOUNT) to count the records and output the count as a single record.
Run another data step to ouptut the data into a file and something like
Code: |
DATA _NULL_;
SET <data records>;
FILE OUTFILE;
IF _N_ = 1
THEN DO;
SET HDRCOUNT;
PUT <header record with count>;
END; |
|
|
Back to top |
|
|
ppandey07
New User
Joined: 27 Nov 2008 Posts: 51 Location: Delhi, India
|
|
|
|
Robert Sample wrote: |
Run a data step (HDRCOUNT) to count the records and output the count as a single record.
Run another data step to ouptut the data into a file and something like
Code: |
DATA _NULL_;
SET <data records>;
FILE OUTFILE;
IF _N_ = 1
THEN DO;
SET HDRCOUNT;
PUT <header record with count>;
END; |
|
How can I run a data step (HDRCOUNT) to count the records and output the count as a single record? |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8696 Location: Dubuque, Iowa, USA
|
|
|
|
Assuming DETAIL is the data set with the detail records:
Code: |
DATA HDRCOUNT;
SET DETAIL END=EOF;
NRECS+1;
IF EOF THEN OUTPUT;
|
or you can use a SET ioption to get the count directly but it's been a long time since I needed to do that so I don't remember the details. You could look it up on the SAS web site, of course. |
|
Back to top |
|
|
ppandey07
New User
Joined: 27 Nov 2008 Posts: 51 Location: Delhi, India
|
|
|
|
ppandey07 wrote: |
Robert Sample wrote: |
Run a data step (HDRCOUNT) to count the records and output the count as a single record.
Run another data step to ouptut the data into a file and something like
Code: |
DATA _NULL_;
SET <data records>;
FILE OUTFILE;
IF _N_ = 1
THEN DO;
SET HDRCOUNT;
PUT <header record with count>;
END; |
|
How can I run a data step (HDRCOUNT) to count the records and output the count as a single record? |
Can you please let me know what should I put in <header record with count>. |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8696 Location: Dubuque, Iowa, USA
|
|
|
|
How do you think I will know what your header record looks like? That should be something you ALREADY know! |
|
Back to top |
|
|
ppandey07
New User
Joined: 27 Nov 2008 Posts: 51 Location: Delhi, India
|
|
|
|
Robert Sample wrote: |
How do you think I will know what your header record looks like? That should be something you ALREADY know! |
Yes you are correct. However, I wanted to ask that how would I use NRECS in my header record. Lets say my header record is like
"The total no. of records as per (today's date) is NRECS"
So how can I get that
Thank you!!! |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8696 Location: Dubuque, Iowa, USA
|
|
|
|
Code: |
CD = TODAY();
FORMAT CD YYMMDDS10.;
PUT 'The total no. of records as per ' CD ' is ' NRECS; |
|
|
Back to top |
|
|
ppandey07
New User
Joined: 27 Nov 2008 Posts: 51 Location: Delhi, India
|
|
|
|
Robert Sample wrote: |
Code: |
CD = TODAY();
FORMAT CD YYMMDDS10.;
PUT 'The total no. of records as per ' CD ' is ' NRECS; |
|
Thank you very much!!! Have a nice day... |
|
Back to top |
|
|
Phrzby Phil
Senior Member
Joined: 31 Oct 2006 Posts: 1042 Location: Richmond, Virginia
|
|
|
|
How about proc sql?
Code: |
select count(*) into :num from file; |
creates a macro variable "num".
The next DATA step then uses %num. |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8696 Location: Dubuque, Iowa, USA
|
|
|
|
The SET statement option NOBS could also be used. |
|
Back to top |
|
|
|