DATA Step, Macro, Functions and more

SAS import data problem

Reply
Contributor
Posts: 21

SAS import data problem

Hi,everyone!

I have a problem about how to import data into sas. My data is a txt file in which there are 5 variables,StockNumber,ObservationNumber, Date,OpenPrice and ClosePrice ,the sample data is follows:

10000 364

19860106 -999 -999

19860107 -66 -999

19860108 -0.024390 -0.024390

19860109 0 0

19860110 0 0

19860113 0.050000 0.050000

19860114 0.047619 0.047619

19860115 0.045455 0.045455

19860116 0.043478 0.043478

10110 1522

19251231 -66 -999

19260102 -0.023810 -0.023810

19260104 0.073171 0.073171

19260105 -0.011364 -0.011364

19260106 -0.034483 -0.034483

19260107 0.023810 0.023810

19260108 0 0

10137 22568

19251231 -66 -999

19260102 -0.010453 -0.010453

19260104 0.035211 0.035211

19260105 -0.008503 -0.008503

19260106 -0.017153 -0.017153

19260107 0 0

19260108 -0.003490 -0.003490

19260109 -0.012259 -0.012259

19260111 -0.003546 -0.003546

19260112 -0.010676 -0.010676

19260113 -0.007194 -0.007194

19260114 0.007246 0.007246

19260115 -0.014388 -0.014388

As you see, the highlighted lines are just two columns which stand for StockNumber,ObservationNumber,then next several rows are Date,OpenPrice and ClosePrice.

It seems that this is not a standard "input"-style data file,and more like concatenated several files into one.would you please help me with that?Thank you very much!

Respected Advisor
Posts: 4,173

Re: SAS import data problem

Something like below might work for you:

data want;

  attrib

    StockNumber       length=$10

    ObservationNumber length=8

    Date              informat=yymmdd. format=date9.

    OpenPrice         length=8

    ClosePrice        length=8

    ;

  retain StockNumber ObservationNumber;

  infile datalines truncover dlm=' ' dsd;

  input @;

  if countw(_infile_,' ')=2 then

    do;

      input StockNumber $ ObservationNumber;

    end;

  else

    do;

      input Date :yymmdd. OpenPrice ClosePrice;

      output;

    end;

  datalines;

10000 364

19860106 -999 -999

19860107 -66 -999

19860108 -0.024390 -0.024390

19860109 0 0

19860110 0 0

19860113 0.050000 0.050000

19860114 0.047619 0.047619

19860115 0.045455 0.045455

19860116 0.043478 0.043478

10110 1522

19251231 -66 -999

19260102 -0.023810 -0.023810

19260104 0.073171 0.073171

19260105 -0.011364 -0.011364

19260106 -0.034483 -0.034483

19260107 0.023810 0.023810

19260108 0 0

10137 22568

19251231 -66 -999

19260102 -0.010453 -0.010453

19260104 0.035211 0.035211

19260105 -0.008503 -0.008503

19260106 -0.017153 -0.017153

19260107 0 0

19260108 -0.003490 -0.003490

19260109 -0.012259 -0.012259

19260111 -0.003546 -0.003546

19260112 -0.010676 -0.010676

19260113 -0.007194 -0.007194

19260114 0.007246 0.007246

19260115 -0.014388 -0.014388

;

run;

Contributor
Posts: 21

Re: SAS import data problem

It works very well!

Thank you very much!

Ask a Question
Discussion stats
  • 2 replies
  • 222 views
  • 1 like
  • 2 in conversation