parse.SAScii: Convert SAS import instructions into the arguments for a...

Description Usage Arguments Details Value Author(s) Examples

Description

Re-configure the INPUT block of a (.sas) syntax file into the arguments necessary to run the read.fwf function on an ASCII data set.

Usage

1
parse.SAScii( sas_ri, beginline = 1 , lrecl = NULL )

Arguments

sas_ri

Character string containing location of SAS import instructions.

beginline

Line number in SAS import instructions where the INPUT statement begins. If the word INPUT appears before the actual INPUT block, the function will return an error.

lrecl

LRECL option from SAS code. Only necessary if the width of the ASCII file is longer than the actual columns containing data (if the file contains empty space on the right side)

Details

This function cannot handle overlapping columns. For example, in the 2009 National Ambulatory Medical Care Survey (NAMCS) SAS import instructions, columns DIAG1 and DIAG13D will create an error because both start at space 55.
ftp://ftp.cdc.gov/pub/Health_Statistics/NCHS/dataset_documentation/namcs/sas/nam09inp.txt.

Value

An R data frame containing:

varname

The name of the variable field

width

The width of the field

char

A logical flag indicating a character field if T and numeric if F

divisor

A fraction to later be multiplied by numeric fields containing decimal points

Author(s)

Anthony Joseph Damico

Examples

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
###########
#Some Data#
###########

#write an example ASCII data set
some.data <- 
	"0154hello2304coolgreatZZ
	2034puppy0023nicesweetok
	9900buddy4495    swell!!"

#create temporary ASCII file
some.data.tf <- tempfile()
#write the sas code above to that temporary file
writeLines ( some.data , con = some.data.tf )

#write an example SAS import script using the @ method
sas.import.with.at.signs <-
	"INPUT
		@1 NUMBERS1 4.2
		@5 WORDS1 $ 5.
		@10 NUMBERS2 2.0
		@12 NUMBERS3 2.0
		@14 WORDS2 $4.
		@18 WORDS3 $5
		@23 WORDS4 $ 1
		@24 WORDS5 $ 1
	;"
	

#create a temporary file
sas.import.with.at.signs.tf <- tempfile()
#write the sas code above to that temporary file
writeLines ( sas.import.with.at.signs , con = sas.import.with.at.signs.tf )

parse.SAScii( sas.import.with.at.signs.tf )

#write an example SAS import script using the dash method
sas.import.with.lengths <-
	"INPUT
		NUMBERS1 1 - 4 .2
		WORDS1 $ 5-9
		NUMBERS2 10 -11
		NUMBERS3 12- 13 .0
		WORDS2 $14-17
		WORDS3$ 18-22
		WORDS4   $   23-23
		WORDS5 $24
	;"
	
#create a temporary file
sas.import.with.lengths.tf <- tempfile()
#write the sas code above to that temporary file
writeLines ( sas.import.with.lengths , con = sas.import.with.lengths.tf )

parse.SAScii( sas.import.with.lengths.tf )


############################################
#Survey of Income and Program Participation#
############################################

#first fourteen lines pulled from the Survey of Income and Program Participation
#http://smpbff2.dsd.census.gov/pub/sipp/2008/l08puw1.sas

sipp.sas <- 
	"LIBNAME sas8 v8 'current directory';
	FILENAME INPUTDAT 'l08puw1.dat' ;
	DATA sas8.l08puw1 ;
	INFILE INPUTDAT PAD LRECL=2341 ;
	INPUT
	 SSUSEQ 1- 5
	 SSUID $ 6- 17
	 SPANEL 18- 21
	 SWAVE 22- 23
	 SROTATON 24- 24
	 SREFMON 25- 25
	 RHCALMN 26- 27
	 RHCALYR 28- 31
	 SHHADID 32- 34
	 GVARSTR 35- 37
	 GHLFSAM 38- 38
	 GRGC $ 39- 41
	 TFIPSST 42- 43
	 TMOVRFLG 44- 45
			; RUN;"

#create a temporary file
sipp.tf <- tempfile()
#write the sas code above to that temporary file
writeLines ( sipp.sas , con = sipp.tf )
#parse that temporary file
sipp.fwf.parameters <- parse.SAScii( sipp.tf , beginline = 5 )
#print the results to the screen
sipp.fwf.parameters

###################################################
#NATIONAL HEALTH INTERVIEW SURVEY - IMPUTED INCOME#
###################################################

#INPUT lines pulled from the 2011 National Health Interview Survey's Imputed Income file
#ftp://ftp.cdc.gov/pub/Health_Statistics/NCHS/Datasets/NHIS/2011_imputed_income/incmimp.sas

nhis.incmimp.sas <- 
	"   * INPUT ALL VARIABLES;

	   INPUT
	  RECTYPE  1-2   SRVY_YR  3-6  
	  HHX   7-12  FMX   13-14
	  FPX   15-16  IMPNUM   17   
	  FAMINCF2 18   TCINCM_F  19
	  FAMINCI2  20-25  POVRATI3  26-34
	  EMPLOY_F  35    EMPLOY_I  36    
	  ERNYR_F  37    TCEARN_F  38
	  ERNYR_I2  39-44
	  ;

	   * DEFINE VARIABLE LABELS;"

#create a temporary file
nhis.incmimp.tf <- tempfile()
#write the sas code above to that temporary file
writeLines ( nhis.incmimp.sas , con = nhis.incmimp.tf )
#parse that temporary file
nhis.incmimp.fwf.parameters <- parse.SAScii( nhis.incmimp.tf )
#print the results to the screen
nhis.incmimp.fwf.parameters


###################################################
#NATIONAL HEALTH INTERVIEW SURVEY - IMPUTED INCOME#
###################################################

#INPUT lines pulled from the 2011 National Health Interview Survey's Sample Adult file
#ftp://ftp.cdc.gov/pub/Health_Statistics/NCHS/Program_Code/NHIS/2011/samadult.sas

nhis.samadult.sas <- 
	"   * INPUT ALL VARIABLES;

	   INPUT

		  /* IDN LOCATIONS */

		  RECTYPE       1 -   2    SRVY_YR       3 -   6
		  HHX      $    7 -  12    INTV_QRT     13 -  13
		  ASSIGNWK     14 -  15    FMX      $   16 -  17
		  FPX      $   18 -  19    WTIA_SA      20 -  26 .1
		  WTFA_SA      27 -  32

		  /* UCF LOCATIONS */

		  REGION       33 -  33    STRAT_P      34 -  36
		  PSU_P        37 -  38
		;"

#create a temporary file
nhis.samadult.tf <- tempfile()
#write the sas code above to that temporary file
writeLines ( nhis.samadult.sas , con = nhis.samadult.tf )
#parse that temporary file
nhis.samadult.fwf.parameters <- parse.SAScii( nhis.samadult.tf )
#print the results to the screen
nhis.samadult.fwf.parameters


## Not run: 

#########################################################################################
#Create the read.fwf parameters required to load the
#2009 Medical Expenditure Panel Survey Emergency Room Visits file

#Location of the SAS import instructions for the
#2009 Medical Expenditure Panel Survey Emergency Room Visits File
MEPS.09.ER.visit.SAS.read.in.instructions <- 
	"http://meps.ahrq.gov/mepsweb/data_stats/download_data/pufs/h126e/h126esu.txt"

#Load the 2009 Medical Expenditure Panel Survey Emergency Room Visits File
#NOTE: The SAS INPUT command occurs at line 273.
MEPS.09.ER.visit.sas <- 
	parse.SAScii ( MEPS.09.ER.visit.SAS.read.in.instructions , beginline = 273 )


#########################################################################################
#Create the read.fwf parameters required to load the
#2011 National Health Interview Survey Persons file

NHIS.11.personsx.SAS.read.in.instructions <- 
	"ftp://ftp.cdc.gov/pub/Health_Statistics/NCHS/Program_Code/NHIS/2011/personsx.sas"

#store the NHIS SAS import instructions for use in a 
#read.fwf function call outside of the read.SAScii function
NHIS.11.personsx.sas <- 
	parse.SAScii( NHIS.11.personsx.SAS.read.in.instructions )

## End(Not run)

SAScii documentation built on May 2, 2019, 9:28 a.m.