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

Dear SAS Community:

I used stattransfer and it put v in front of all my ticker symbols.

I am trying to remove the letter v in front of the variable name.  So for example if the _NAME_ is "v88890" I would like to remove the "v" and _NAME_ to be "88890". However if _NAME_ is "B1HHK8" then nothing is removed. So v is removed only at the beginning of _NAME_. The length of _NAME_ is maximum of 6 characters.

How do I achieve these efficiently.

data have;
  infile datalines dsd truncover;
  input C1:MMDDYY8. _NAME_:$8. COL1:32.;
  format C1 MMDDYY8.;
  label _NAME_="NAME OF FORMER VARIABLE";
datalines4;
12/30/05,v88890,9.794
12/30/05,v83818,70.151
12/30/05,v17015,541.057
12/30/05,v56453,637.7
12/30/05,v65994,79.277
12/30/05,v92915,33.648
12/30/05,v63487,38.452
12/30/05,v14527,82708.5
12/30/05,v339429,341.40265309
12/30/05,B011MX,152.421
12/30/05,v55837,620.948
12/30/05,v91027,565.622
12/30/05,B1HHK8,2.861
12/30/05,B1SN2C,805.965
12/30/05,v55077,64.756
12/30/05,B1B59F,1037
12/30/05,B049FG,69.544779936
12/30/05,v17423,534.259
12/30/05,v88124,87.264
12/30/05,v302210,131.136
12/30/05,v37791,257.75040253
12/30/05,v307940,111.416
12/30/05,v65827,133.831
12/30/05,B0BHKX,20.020925945
12/30/05,v69417,133.4
12/30/05,v25703,373.8
12/30/05,B140VT,0.8659560285
12/30/05,v315395,51.922
12/30/05,v9315,618.4
12/30/05,v308266,221.547
12/30/05,B0XZZ5,481.655
12/30/05,v65611,536.353
12/30/05,v53160,332.652
12/30/05,v68295,45.755
12/30/05,v56692,81.114
12/30/05,v16877,11.823
12/30/05,v69116,35.815
12/30/05,B03105,14.151
12/30/05,v314231,551.815
12/30/05,B0CD5F,20.735094553
12/30/05,v96454,37.302
12/30/05,B01NJN,50.59851504
12/30/05,B0K32V,
12/30/05,B02WRR,70.861
12/30/05,v14699,103.252
12/30/05,B2PF6M,10992
12/30/05,v301974,44.628
12/30/05,v17665,183.436
12/30/05,B06FN1,828.23766499
12/30/05,v29050,85.106
12/30/05,B05L0T,21.85
12/30/05,v26150,371.7
12/30/05,B1VVM6,
12/30/05,v29743,110.275
12/30/05,v89750,4.950383
12/30/05,v317297,578.969
12/30/05,BRTL8Q,
12/30/05,v23034,1772.8
12/30/05,B0WM2V,87.098
12/30/05,v89626,2578.3
12/30/05,v332520,435.539
12/30/05,B4KHXB,1.896
12/30/05,v27817,29.137697
12/30/05,v67250,195.952
12/30/05,v79811,460.5
12/30/05,B3CG9C,38.132
12/30/05,B09BY4,331.4
12/30/05,v84354,836.38717513
12/30/05,B1G527,31.441
12/30/05,B0L2JD,53.901
12/30/05,v5806,45.767
12/30/05,B28TMS,160.118
12/30/05,v18036,1925.918
12/30/05,B1VWM1,131.067
12/30/05,B4KNNH,
12/30/05,B58HZJ,3192.833
12/30/05,B084LP,
12/30/05,v68165,27.047
12/30/05,v2826,5503.900456
12/30/05,v10229,70.198
12/30/05,v25025,18.786664884
12/30/05,v335631,100.18523453
12/30/05,B2Q4TN,46.801535251
12/30/05,B138NB,1336.9
12/30/05,B28B28,
12/30/05,BYY8NN,
12/30/05,BMH465,11.324
12/30/05,B0L4LM,51.006844736
12/30/05,BG86C0,0.614
12/30/05,v27336,
12/30/05,BMSKPJ,
12/30/05,BWC53H,
12/30/05,B7VSCF,
12/30/05,B7V2GY,
12/30/05,B9B3DY,
12/30/05,B1XZS8,30029.416288
12/30/05,BD0Q3L,2.197992
12/30/05,v2813,169.6
12/30/05,B3ZHFD,168.3
12/30/05,B085SD,1.433
;;;;
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

One way for the example data:

data have;
  infile datalines dsd truncover ;
  input C1:MMDDYY8. _NAME_:$8. COL1:32.;
  format C1 MMDDYY8.;
  label _NAME_="NAME OF FORMER VARIABLE";
  if _NAME_ =:'v' then _NAME_= substr(_NAME_,2);
datalines4;
12/30/05,v88890,9.794
12/30/05,v83818,70.151
12/30/05,v17015,541.057
12/30/05,v56453,637.7
;;;;
run;

There is a question of do you want to remove a capitol V as well?

 

The =:  in the If is asking does the value on the right of the =: start with the given string. The string can be longer or even another variable.

View solution in original post

4 REPLIES 4
Reeza
Super User
if substr(_name_, 1, 1) = 'v' then _name_ = substr(_name_,2);

Or a better approach is to go back to your transpose step and and see if you can have the label transposed. I highly suspect the labels have the values you want since SAS variable names can't be numeric.
ballardw
Super User

One way for the example data:

data have;
  infile datalines dsd truncover ;
  input C1:MMDDYY8. _NAME_:$8. COL1:32.;
  format C1 MMDDYY8.;
  label _NAME_="NAME OF FORMER VARIABLE";
  if _NAME_ =:'v' then _NAME_= substr(_NAME_,2);
datalines4;
12/30/05,v88890,9.794
12/30/05,v83818,70.151
12/30/05,v17015,541.057
12/30/05,v56453,637.7
;;;;
run;

There is a question of do you want to remove a capitol V as well?

 

The =:  in the If is asking does the value on the right of the =: start with the given string. The string can be longer or even another variable.

ChrisNZ
Tourmaline | Level 20

Function first() can also be used in place of substr( ,1,1)  or  =:  .

So many choices!

ChrisBrooks
Ammonite | Level 13

You can also do it with a regular expression e.g.

 

data want(drop=prx);
	set have;
	if _n_=1 then do;
		retain prx;
		prx=prxparse("s/^v//");
	end;
	_name_=prxchange(prx,1,_name_);
run;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1142 views
  • 4 likes
  • 5 in conversation