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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

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