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

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:

DX1DX2DX3DX4DX5DX6DX7
F15.151S32.602A      
S72.401BT79.4XXAR78.81G82.20S22.39XAJ96.90M79.5 
W34.00XAR57.0N28.9     
S81.801AK21.9E87.6S71.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!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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.;

View solution in original post

3 REPLIES 3
Reeza
Super User

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.;
Tom
Super User Tom
Super User

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.

 

novinosrin
Tourmaline | Level 20
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;

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

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 720 views
  • 1 like
  • 4 in conversation