Home > Blockchain >  Break the character values into multiple names in SAS
Break the character values into multiple names in SAS

Time:07-23

Suppose I have a data set as follows

data mydat; 
  input samplename$ value;
  datalines;
  AA:77:D1 45
  BB:08:D3 50
;
run;

I would like to separate the 'samplename' variable values into three part with three new variable. Expected outcome is enter image description here

CodePudding user response:

For your particular task you can use the folowing code:

data mydat; 
  length  v1 v2 v3 $200; 
  input v1 v2 v3 value;
  infile cards dlm=": ";
  datalines;
  AA:77:D1 45
  BB:08:D3 50
;
run;

Tom has absolutely correctly suggested more traditional and might be flexible for the changes way:

data mydat; 
  length samplename v1 v2 v3 $200; 
  input samplename$ value;
  v1 = scan (samplename, 1, ':');
  v2 = scan (samplename, 2, ':');
  v3 = scan (samplename, 3, ':');
  datalines;
  AA:77:D1 45
  BB:08:D3 50
;
run;

CodePudding user response:

For the case of samplename data values always having three parts you can specify dlm=': '

Example:

data want; 
  infile datalines dlm=': ';
  input (var1-var3) ($) value;
  datalines;
  AA:77:D1 45
  BB:08:D3 50
;
run;
  • Related