BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Hima
Obsidian | Level 7

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15

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

1 REPLY 1
Haikuo
Onyx | Level 15

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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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