Help using Base SAS procedures

Multiple data sets needs to be created from Raw data based on a condition

Accepted Solution Solved
Reply
Regular Contributor
Posts: 233
Accepted Solution

Multiple data sets needs to be created from Raw data based on a condition

MuI need to create 3 data sets from the below raw data.

COL1  COL2 COL3 COL4

111      111   DDD  EEE

222      111   RRR  TTT

BAT       23

124      158   FFF  FFF

444      454   MMM  MMM

BAT       26

555       121   LLL  TTT

122       158   JJJ  MMM

BAT       33

My first data set should be as follows

COL1  COL2 COL3 COL4

111      111   DDD  EEE

222      111   RRR  TTT

BAT       23

My second data set should be as follows

COL1  COL2 COL3 COL4

124      158   FFF  FFF

444      454   MMM  MMM

BAT       26

My third data set should be as follows

COL1  COL2 COL3 COL4

555       121   LLL  TTT

122       158   JJJ  MMM

BAT       33

So the data set seperator here is the word BAT. I need to create data set of such records that are above the word BAT.


Accepted Solutions
Solution
‎05-21-2012 03:37 PM
Respected Advisor
Posts: 3,156

Re: Multiple data sets needs to be created from Raw data based on a condition

Hi,

Hash() seems to have an edge in term of dynamic outputting dataset:

data _null_;

length col1-col4 $8.;

  if _n_=1 then do;

  dcl hash h(ordered :'a');

h.definekey('n');

h.definedata('col1', 'col2', 'col3', 'col4');

h.definedone();

end;

do until (col1='BAT');

infile cards truncover;

input (COL1 COL2 COL3 COL4) (:$8.);

n+1;

h.add();

end;

flag+1;

h.output(dataset: 'w'||strip(put(flag,best.)));

h.clear();

cards;

111 111 DDD EEE

222 111 RRR TTT

BAT 23

124 158 FFF FFF

444 454 MMM MMM

BAT 26

555 121 LLL TTT

122 158 JJJ MMM

BAT 33

;

Regards,

Haikuo

Edit: you need to replace 'cards' with your physical file name or reference.

View solution in original post


All Replies
Solution
‎05-21-2012 03:37 PM
Respected Advisor
Posts: 3,156

Re: Multiple data sets needs to be created from Raw data based on a condition

Hi,

Hash() seems to have an edge in term of dynamic outputting dataset:

data _null_;

length col1-col4 $8.;

  if _n_=1 then do;

  dcl hash h(ordered :'a');

h.definekey('n');

h.definedata('col1', 'col2', 'col3', 'col4');

h.definedone();

end;

do until (col1='BAT');

infile cards truncover;

input (COL1 COL2 COL3 COL4) (:$8.);

n+1;

h.add();

end;

flag+1;

h.output(dataset: 'w'||strip(put(flag,best.)));

h.clear();

cards;

111 111 DDD EEE

222 111 RRR TTT

BAT 23

124 158 FFF FFF

444 454 MMM MMM

BAT 26

555 121 LLL TTT

122 158 JJJ MMM

BAT 33

;

Regards,

Haikuo

Edit: you need to replace 'cards' with your physical file name or reference.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 1 reply
  • 167 views
  • 0 likes
  • 2 in conversation