DATA Step, Macro, Functions and more

Split data and pad with zero's

Reply
Occasional Contributor
Posts: 15

Split data and pad with zero's

Good Afternoon,

i need some insight please! Smiley Happy

 

what im trying to do is, read a column that needs to be seperated.

the column reads  3456789999

 

what i need to do is pad the first column

 

so i would split it in two clumns instr(columnA,4,7) to get 6789999

the 2nd column would need to be 0345 as the column needs to be 4 bytes.

and i want to concatenate the result  0345-6789999

 

 

PROC Star
Posts: 7,363

Re: Split data and pad with zero's

Is the current column character or numeric?

 

Art, CEO, AnalystFinder.com

 

Occasional Contributor
Posts: 15

Re: Split data and pad with zero's

hi art,

 

the file im reading isnumeric,

end result as it has a dash, will need to be text which can have leading zero's

PROC Star
Posts: 7,363

Re: Split data and pad with zero's

Here is one way:

data have;
  input columna;
  cards;
3456789999
;

data want;
  set have;
  columnb=substr(strip(put(columna,best32.)),4);
  columnc=substr(put(columna,z11.),1,3);
  columnd=catx('-',columnc,columnb);
run;

Art, CEO, AnalystFinder.com

 

Occasional Contributor
Posts: 15

Re: Split data and pad with zero's

hi art,

 

the file im reading is numeric,

end result as it has a dash, will need to be text which can have leading zero's

Highlighted
Super User
Super User
Posts: 6,499

Re: Split data and pad with zero's

You can use the Z format to make an 11 character string with leading zeros and then add the hyphen.

Or you could create a picture format.  With the picture format you could leave the variable as numeric if you wanted.

proc format ;
  picture id low-high='9999-9999999' other=' ';
run;

data text ;
  length x1 x2 8 c1 c2 $12;
  x1=3456789999 ;
  x2=x1;
  format x1 z11. x2 id. ;
  c1=put(x1,Z11.);
  c1=catx('-',substr(c1,1,4),substr(c1,5));
  c2=put(x1,id.);
  put (_all_) (=/);
run;
Ask a Question
Discussion stats
  • 5 replies
  • 105 views
  • 0 likes
  • 3 in conversation