BookmarkSubscribeRSS Feed
radhikaa4
Calcite | Level 5

I want to just pick out the middle instance of "have_id"

 

have_idwant_id
1234-ABCD-5678ABCD
6564-DGFD-4958DGFD

 

Currently I used 

proc sql;

select have_id, right(left(subjectID, 9), 4) as want_id

from testdata; quit;

 

it works in my sql server but not in the SAS. any help would be great!

3 REPLIES 3
novinosrin
Tourmaline | Level 20

Hi @radhikaa4 

 

data have;
input have_id $30.;
cards;	
1234-ABCD-5678	
6564-DGFD-4958	
;

data want;
set have;
want_id=scan(have_id,median(1,countw(have_id,'-')),'-');
run;
ballardw
Super User

If your values are all as simple as you display then

 

scan(have_id,2,'-') as want_id

should work.

This assumes that you always have at least 1 hyphen character and that the part you want is bit immediately after the first hyphen. The hyphen in the scan is telling the SCAN function to use only the hyphen as a word delimiter so other characters such as space, * , slash , period, question mark and most punctuation would be ignored for determining word boundaries. If you have other characters that need to be considered then a more comprehensive example should be provided.

Tom
Super User Tom
Super User

@radhikaa4 wrote:

I want to just pick out the middle instance of "have_id"

 

have_id want_id
1234-ABCD-5678 ABCD
6564-DGFD-4958 DGFD

 

Currently I used 

proc sql;

select have_id, right(left(subjectID, 9), 4) as want_id

from testdata; quit;

 

it works in my sql server but not in the SAS. any help would be great!


LEFT() and RIGHT() do not take any numeric arguments, just the string that want to left align or right align.

 

Why wouldn't you just use the SUBSTR() function in either SQL server or SAS? 

substr(subjectid,6,4)

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 3 replies
  • 288 views
  • 0 likes
  • 4 in conversation