Thursday, October 25, 2007

Data Step

/* database file */ filename class 'c:\data\class.dbf'; proc dbf db4=class out=class; run; /* MS access file */ libname file 'c:\data\filename.mdb'; proc print data=file.table; run; /* proc import */ /* DLM indicates a delimited file with the default delimiter of a blank */
PROC IMPORT
  datafile='pathname' out=mylib.data dbms=dlm replace;
  delimiter='!';
  getnames=yes;

/* csv file */
PROC IMPORT
  datafile='pathname' out=mylib.data dbms=csv replace;
  getnames=yes;
/* proc export (csv) */ proc export data=sasuser.houses outfile="/myfiles/class.csv" dbms=csv; /* input */ input (x1-x35)($1.); input name $10. +3 x1 7. x2 6. x3 6.; input (name x1-x3)($10. +3 7. 2*6.); input (name x1-x3)($10. +3 7. 2*6.) (y1-y5)(2*6. 3. 2*8.); /* character -> numeric */ input(num, 8.0); /* numeric -> character */ left(char); put(char, 1.) /* input 2*3 table */

do i='No','Yes';
do i=1,2,3,4;

data twobythree;
  do i = 1 to 2;
    do j =1 to 3;
      input x @@; output;
    end;
  end;
cards;
11 12 13 21 22 23
run;

/* datastep for 1 obs for a line */
data methods;
  input irrig $ @@;
  do bloc=1 to 8;
    input fruitwt @@;
    logfwt=log(fruitwt); output;
  end;
  datalines;
trickle 450 469 249 125 280 352 221 251
basin 358 512 281 58 352 293 283 186
spray 331 402 183 70 258 281 219 46
sprnkler 317 423 379 63 289 239 269 357
flood 245 380 263 62 336 282 171 98
run;

/* generate 2 datasets */
data males females;
  set class;
  if sex='M' then output males;
  else output females;
run;


/* Missing */
data class;
  set aa;
  array x _numeric_;
  do over x;
    if x=999 then x=.; /* missing */
  end;
run;

/* random number */
data random(drop=n);
  do n=1 to 10;
    x=uniform(0); /* 0 means the seed number depends on current time */
    output;
  end;
run;
/* random number generator */ rannor(seed); ranuni(seed); uniform(seed); ranbin(seed,n,p); rancau(seed); ranexp(seed); rangam(seed, alpha); ranpoi(seed, lambda); rantbl(seed, p1, p2...,pn);

No comments: