BookmarkSubscribeRSS Feed
Maneesh27
Calcite | Level 5

The length of this text is a very long text. As per CDISC standards, all variables in datasets should have a maximum length of 200 characters. As a consequence, this text needs to be split into variables TERM1, TERM2, ... TERMX, with each having 200 characters long.
2 REPLIES 2
Patrick
Opal | Level 21

Something like below should work.

data have;
  row_id=_n_;
  length row_id 8 term $2000;
  row_id=1;
  term='The length of this text is a very long text. As per CDISC standards, all variables in datasets should have a maximum length of 200 characters. As a consequence, this text needs to be split into variables TERM1, TERM2, ... TERMX, with each having 200 characters long.';
  output;
  row_id=2;
  term=repeat('aaa bbb ',180);
  output;
run;

data split_200(keep=row_id term_:);
  length term_200 $200;
  set have;
  _pos_start=1;
  _pos_stop =_pos_start+200;

  do term_no=1 by 1;
    _pos_found=findc(term,' ',-_pos_stop);
    term_200  =substr(term,_pos_start,_pos_found-_pos_start);
    if missing(term_200) then leave;
    output;
    _pos_start=_pos_found+1;
    _pos_stop =_pos_start+200;
  end;
run;

proc transpose data=split_200 out=split_200_transp(drop=_name_) prefix=term_;
  by row_id;
  id term_no;
  var term_200;
run;

data want;
  merge have split_200_transp;
  by row_id;
run;

proc print data=want;
run;

Patrick_0-1716973868557.png

 

 

Patrick
Opal | Level 21

@Maneesh27 Code in previous post just amended with a corrected expression for the substr() function.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 925 views
  • 0 likes
  • 2 in conversation