BookmarkSubscribeRSS Feed
xxz3231
Calcite | Level 5

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!

2 REPLIES 2
Patrick
Opal | Level 21

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;

xxz3231
Calcite | Level 5

It works very well!

Thank you very much!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 911 views
  • 1 like
  • 2 in conversation