DATA Step, Macro, Functions and more

How to spilt a value into three variables

Reply
Super Contributor
Posts: 272

How to spilt a value into three variables

Dear,

 

in my raw data, a variable value contains characters longer than 200 characters in length. I need split the value into variable1, variable2, and so on depends on the number of charracters and each variable can contain maximum of 200 characters. Thank you.

 

data

 

Variable

Site to document the deviation in the subject source. Sleep technician will receive addition protocol training for scoring requirements and procedures. Site reported the deviation to the investigator on 20JUN2013.Subject 32 Screening Mean sleep latency could not be determined for this subject as sleep latency was unknown for all of the MWT naps.

Super User
Posts: 5,496

Re: How to spilt a value into three variables

Posted in reply to knveraraju91

Here's a recently asked question that only splits into 2 variables instead of 3 ... but all the same techniques would apply.

 

https://communities.sas.com/t5/General-SAS-Programming/Split-variable-gt-200-char-into-fragments-lt-...

 

Trusted Advisor
Posts: 1,553

Re: How to spilt a value into three variables

Posted in reply to knveraraju91

You may try:

length var1-var5 $200;   /* assume max 5 variables to output */

array varx var1-var5;

 

var_len = length(var_in);

do i=1 to 5; varx(i)=' '; end;

 

if var_len le 200 then var1 = var_in;  

else do ;

        i=1;

        do until (var_len < 200);

             varx(i) = substr(var_in,(i-1)*200 +1, min(var_len, 200));

             var_len = var_len - 200;

             i+1;

       end;

 

       

Trusted Advisor
Posts: 1,553

Re: How to spilt a value into three variables

oops, change the do until (var_len < 200); into
DO WHILE (VAR_LEN > 0(;
Ask a Question
Discussion stats
  • 3 replies
  • 256 views
  • 1 like
  • 3 in conversation