Methods to access data in SAS

REFERENCE TO EXTERNAL FILE VIA LIBNAME ENGINE

LIBNAME {libref} {DBMS descriptor} {dir to file}
DBMS descriptor for Excel: XLSX, XLS, EXCEL
DBMS descriptor for csv: CSV
DBMS descriptor for txt: DLM

libname mylib xlsx "C:\Data\height.xlsx";
proc print data=mylib.sheet1;
run;
libname testlbr xlsx "C:\Data\height.xlsx";
libname outlib "F:\SAS\work\Studies";
data outlib.mysheet;
set testlbr.sheet1;
run;

READ INTERNAL RAW DATA

data outds;
input Name $ Age Score;
datalines;
Jack 18 90
Mike 8 49
Calline 24 76
;
run;

READ EXTERNAL RAW DATA WITH INFILE LIST INPUT

DATA {filename for imported file};
INFILE {dir to external file} DLM={delimiter} DSD LRECL={length of longest record in the input data}; * DSD (delimiter-sensitive data) when data values are enclosed in quotation marks, delimiters within the value are treated as character data.
INPUT {var1} {var2} {var3};

data t3;
infile "C:\Data\band2.csv" dlm="," dsd;
format BandName $18.;
input BandName $ EightPM NinePM TenPM ElevenPM;
run;

READ EXTERNAL RAW DATA WITH INFILE COLUMN INPUT

data t3;
infile "C:\Data\band2.csv";
input BandName $ 1-16 EightPM 17-18 NinePM 19-20 TenPM 21-22 ElevenPM 23-25;
run;
data t4;
infile "C:\Data\pumpkin.txt";
input name $17. age 3. type $2. date MMDDYY10. (J1 J2 J3) (4.1);
run;

@'character' column pointer: Input data at char locator

@"breed" DogBreed $; * Read until it encounters the keyword "breed", applicable when DogBreed clomun always starts with the keyword "breed";

Colon modifier: Input data until space or EOL

@"breed" DogBreed :$20.; * Read until it encounters the keyword "breed" and continue until next space or end of line;

Ampersand modifier: Input data until two spaces

input Cname & $20. * Read until it encounters two spaces or reaches the spcified length limit;
@"School" School $
@"Time" Time :STIMER8.

READ MULTIPLE LINES OF DATA PER OBSERVATION WITH SLASH "/" AND POUND SIGN "#"

data t6;
infile "C:\Data\temp.csv";
input City $ State $
	  / NH NL
	  / RH RL
    ;
run;

READ MULTIPLE OBSERVATIONS PER LINE OF RAW DATA WITH DOUBLE AT SIGN "@@"

data t7;
infile "C:\Data\vac.txt";
input City $ State $ ar ap @@;
run;

PROC IMPORT

PRO IMPORT DATAFILE="{ dir to external file}" OUT={filename for imported file} DBMS={data format} REPLACE;
SHEET={name of sheet to import};
GETNAMES={yes|no};
MIXED={Yes|No}; * Yes: convert numeric values as characters instead of missing values
DATAROW={first row to import from};
GUESSINGROWS={No} * if data file has all missing values or non-representative data in the first 20 data rows

posted @ 2023-09-11 15:55  helloyuen  阅读(5)  评论(0编辑  收藏  举报