DATA Step, Macro, Functions and more

How can I read data with header from the file

Reply
N/A
Posts: 0

How can I read data with header from the file

Hi, I am new to SAS datastep. I found trouble when reading data from a file.

If data formated in the file looks like the following:

OBS CITY TMR SMIN SMEAN
1 PROVIDEN 1096 30 163
2 JACKSON 789 29 70
3 JOHNSTOW 1072 88 123

How can I read directly from the file and form a database in SAS?

Without the column header, I know the statement might be:
infile "xxx";
input obs city $ tmr smin smean;

However, how can I skip the first line of the file when reading data? or is it possible to directly take the column header as the variables?

Thank you for your help in advance.
Respected Advisor
Posts: 3,887

Re: How can I read data with header from the file

Use the data import wizard in SAS EG - it generates the code for you.
Regular Contributor
Posts: 151

Re: How can I read data with header from the file

You can skip the first line by using firstobs option in the infile statement:

infile 'xxx' firstobs=2;
N/A
Posts: 0

Re: How can I read data with header from the file

Thanks. I could do it.
Respected Advisor
Posts: 3,777

Re: How can I read data with header from the file

> or is it possible to directly take
> the column header as the variables?

Here is one way.

[pre]
filename FT15F001 temp;
proc import datafile=FT15F001 dbms=dlm out=test replace;
delimeter = ' ';
parmacrds;
OBS CITY TMR SMIN SMEAN
1 PROVIDEN 1096 30 163
2 JACKSON 789 29 70
3 JOHNSTOW 1072 88 123
;;;;
run;
proc print;
run;
[/pre]
Valued Guide
Posts: 2,174

Re: How can I read data with header from the file

interesting demo!
Is there some interaction between unit 15 (ft15f001) on TEMP and PARMCARDS?
Respected Advisor
Posts: 3,777

Re: How can I read data with header from the file

> interesting demo!
> Is there some interaction between unit 15 (ft15f001)
> on TEMP and PARMCARDS?

I need to learn to spell and read the log.

FT15F001 is the default fileref for PARMCARDS

[pre]
47 proc options option=parmcards value define;
48 run;

SAS (r) Proprietary Software Release 9.1 TS1M3

Option Value Information For SAS Option PARMCARDS
Option Value: FT15F001
Option Scope: Default
How option value set: Shipped Default
Option Definition Information for SAS Option PARMCARDS
Group= ENVFILES
Group Description: SAS library and file location information
Description: Fileref for the PARMCARDS file
Type: The option value is of type CHARACTER
Maximum Number of Characters: 2048
Casing: The option value is retained with original casing.
Quotes: If present during "set", start and end quotes are removed
Parentheses: The option value does not require enclosure within parentheses. If present, the parentheses are
retained.
Expansion: Environment variables are not shown in expanded form.
When Can Set: Startup or anytime during the SAS Session
Restricted: Your Site Administrator can restrict modification of this option.
Optsave: Proc Optsave or command Dmoptsave will save this option.
[/pre]

You don't have to use a temp file and if you just use PARMCARDS without defining FT15F001 you will get a file named FT15F001.DAT in the default directory.

[pre]
50 proc import datafile=FT15F001 dbms=dlm out=test replace;
51 delimiter = ' ';
52 parmcards;
53 OBS CITY TMR SMIN SMEAN
54 1 PROVIDEN 1096 30 163
55 2 JACKSON 789 29 70
56 3 JOHNSTOW 1072 88 123
57 ;;;;

58 /**********************************************************************
59 * PRODUCT: SAS
60 * VERSION: 9.1
61 * CREATOR: External File Interface
62 * DATE: 03OCT09
63 * DESC: Generated SAS Datastep Code
64 * TEMPLATE SOURCE: (None Specified.)
65 ***********************************************************************/
66 data TEST ;
67 %let _EFIERR_ = 0; /* set the ERROR detection macro variable */
68 infile FT15F001 delimiter = ' ' MISSOVER DSD firstobs=2 ;
69 informat OBS best32. ;
70 informat CITY $8. ;
71 informat TMR best32. ;
72 informat SMIN best32. ;
73 informat SMEAN best32. ;
74 format OBS best12. ;
75 format CITY $8. ;
76 format TMR best12. ;
77 format SMIN best12. ;
78 format SMEAN best12. ;
79 input
80 OBS
81 CITY $
82 TMR
83 SMIN
84 SMEAN
85 ;
86 if _ERROR_ then call symputx('_EFIERR_',1); /* set ERROR detection macro variable */
87 run;

NOTE: The infile FT15F001 is:
File Name=C:\Documents and Settings\"data _null_"\My Documents\My SAS Files\9.1\FT15F001.DAT,
RECFM=V,LRECL=256

NOTE: 3 records were read from the infile FT15F001.
The minimum record length was 19.
The maximum record length was 22.
NOTE: The data set WORK.TEST has 3 observations and 5 variables.
[/pre]
Ask a Question
Discussion stats
  • 6 replies
  • 238 views
  • 0 likes
  • 5 in conversation