Hi Folks:
I'd like to substring the first 3 digits of multiple variables all initialized with dx. In real dataset, I have dx01-25. My code posted below doesn't work.
Could you please help correct the existing code or suggest alternative approach to substring all dx01-dx25 without substr each variable individually?
Thanks for your time in advance.
data have;
input dx01 $ dx02 $ dx03 $ case;
cards;
15701 1576 2007 1
15701 2006 1007 1
10001 1576 1007 1
;
data want; set have;
array diag[*] dx01-dx03;
do i = 1 to dim(diag);
dx=diag[i];
DX_substr=substr(left(dx),1,3);
end;
drop i;
run;
Like this?
data HAVE;
input DX01 $ DX02 $ DX03 $ CASE;
cards;
15701 1576 2007 1
15701 2006 1007 1
10001 1576 1007 1
;
data WANT;
set HAVE;
array DXIN [3] DX01-DX03;
array DXOUT[3] ;
do I = 1 to dim(DXIN);
DXOUT[I]=substr(left(DXIN[I]),1,3);
end;
drop I;
run;
DX01 | DX02 | DX03 | CASE | DXOUT1 | DXOUT2 | DXOUT3 |
15701 | 1576 | 2007 | 1 | 157 | 157 | 200 |
15701 | 2006 | 1007 | 1 | 157 | 200 | 100 |
10001 | 1576 | 1007 | 1 | 100 | 157 | 100 |
Like this?
data HAVE;
input DX01 $ DX02 $ DX03 $ CASE;
cards;
15701 1576 2007 1
15701 2006 1007 1
10001 1576 1007 1
;
data WANT;
set HAVE;
array DXIN [3] DX01-DX03;
array DXOUT[3] ;
do I = 1 to dim(DXIN);
DXOUT[I]=substr(left(DXIN[I]),1,3);
end;
drop I;
run;
DX01 | DX02 | DX03 | CASE | DXOUT1 | DXOUT2 | DXOUT3 |
15701 | 1576 | 2007 | 1 | 157 | 157 | 200 |
15701 | 2006 | 1007 | 1 | 157 | 200 | 100 |
10001 | 1576 | 1007 | 1 | 100 | 157 | 100 |
Another way:
data WANT;
set HAVE;
array DXIN DX01 - DX03 ;
array DXOUT $3 DXO01 - DXO03 ;
do over DXIN;
DXOUT=DXIN;
end;
run;
Depending on what need the truncated versions for you may need even need to change the data. Use a format for display or group creation purposes:
data HAVE; input DX01 $ DX02 $ DX03 $ CASE; cards; 15701 1576 2007 1 15701 2006 1007 1 10001 1576 1007 1 ; proc print data=have; format dx: $3.; run;
The groups created by using a different format would be honored in most of the analysis, reporting or graphic procedures.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.