BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Cruise
Ammonite | Level 13

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

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

 

 

 

View solution in original post

4 REPLIES 4
ChrisNZ
Tourmaline | Level 20

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

 

 

 

Cruise
Ammonite | Level 13
Thanks. Exactly!
ChrisNZ
Tourmaline | Level 20

Another way:

data WANT; 
  set HAVE;
  array DXIN     DX01  - DX03  ;
  array DXOUT $3 DXO01 - DXO03 ;
  do over DXIN;
    DXOUT=DXIN;
  end;
run;

 

ballardw
Super User

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.

SAS Innovate 2025: Call for Content

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 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 1376 views
  • 4 likes
  • 3 in conversation