1

My dataset looks like this

Variable image

And I want it to look like this:

 Subject Code  site          subj
 0156 00062    156            62 
 0156 00062    156            62 
 0047 00032    47             32
 0034 00066    34             66
 0032 00029    32             29 
 .
 .  
  • My Code:
if "Subject Code"n ^="" then site=input(scan("Subject Code"n,1,' '),z9.);       
   put site=;       
if "Subject Code"n ^="" thensubj=input(strip(substr((scan("Subject Code"n,-1)),1,4)),$4.);      
put subj=;

The output I get:

site=15600062     
subj=1560

As you can see SAS takes out the leading 0 values and the space " ", because of which it's difficult to split.

Community
  • 1
  • 1
  • Why are you converting the values into numbers if you want to keep the leading 0 characters? – Tom Dec 20 '19 at 22:51

2 Answers2

0

You might be over complicating. Try:

length site subj 8;   * declare the variables as numeric;

site = input (scan ('Subject Code'n,1), 8.);
subj = input (scan ('Subject Code'n,2), 8.);

The variables will need a z format if you want the values to be displayed with leading zeros when rendered in viewers or proc output.

format site z4.;
format subj z5.;
Richard
  • 25,390
  • 3
  • 25
  • 38
0
data have;
input subjectcode $&10.;
datalines;
0156 00062
0156 00062
0047 00032
0034 00066
0032 00029
;

data want;
   set have;
   site=prxchange('s/0*([1-9]+) 0*([1-9]+)/$1/', -1, subjectcode);
   subj=prxchange('s/0*([1-9]+) 0*([1-9]+)/$2/', -1, subjectcode);
run;
PeterClemmensen
  • 4,018
  • 3
  • 14
  • 22