- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hello,
I would like to create a more efficient code for trying to truncate dozens of variables in a list. So, for instance I have this:
DX1 | DX2 | DX3 | DX4 | DX5 | DX6 | DX7 | … |
F15.151 | S32.602A | ||||||
S72.401B | T79.4XXA | R78.81 | G82.20 | S22.39XA | J96.90 | M79.5 | |
W34.00XA | R57.0 | N28.9 | |||||
S81.801A | K21.9 | E87.6 | S71.132A | ||||
… |
And would like just the first three characters from each string: F15, S32, S72, T79, etc.
Is there a way to do this without individually creating new variables for each of the dx vars using the SUBSTR function? In actuality I have 79 of these Dx variables so that would be a time consuming process, and I imagine there's a better way.
Thanks!
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Use an array if you want to do it in place or convert. The $3. creates the new variables with only three characters so that should help.
data want; set have; array dx(25) dx1-dx25; array _newdx(25) $3. new_dx1-new_dx25; do i=1 to dim(dx); _newdx(i) = dx(i); end; run;
If this is just for appearances, you can also just apply the format $3. to all the variables.
format dx1-dx25 $3.;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Use an array if you want to do it in place or convert. The $3. creates the new variables with only three characters so that should help.
data want; set have; array dx(25) dx1-dx25; array _newdx(25) $3. new_dx1-new_dx25; do i=1 to dim(dx); _newdx(i) = dx(i); end; run;
If this is just for appearances, you can also just apply the format $3. to all the variables.
format dx1-dx25 $3.;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
If you just want to convert the VALUES and not change the variable definitions then use an ARRAY.
data want ;
set have;
array dx dx1-dx25;
do i=1 to dim(dx);
dx(i)=substr(dx(i),1,3);
end;
run;
Note this is good example where the DO OVER syntax is useful, although for some reason SAS has removed it from the documentation. DO OVER is useful where the index value has no meaning since you do not need to use a variable for the index.
data want ;
set have;
array dx dx1-dx25;
do over dx;
dx=substr(dx,1,3);
end;
run;
Since the documentation is gone note that this type of implicit array reference like this will actually use the index variable associated with the array. The default is to use _I_ as that variable. So DO OVER is basically saying DO _I_=1 to 25.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data have;
infile cards truncover;
input (DX1 DX2 DX3 DX4 DX5 DX6 DX7) (:$10.);
cards;
F15.151 S32.602A
S72.401B T79.4XXA R78.81 G82.20 S22.39XA J96.90 M79.5
W34.00XA R57.0 N28.9
S81.801A K21.9 E87.6 S71.132A
;
data want;
set have;
array d dx:;
do _n_=1 to dim(d);
d(_n_)=peekclong(addrlong(d(_n_)),3);
end;
run;