BookmarkSubscribeRSS Feed
laneylaners
Obsidian | Level 7

I have the following code:

 

Data work.data;
length A $40;
infile datalines dsd;
Input A $;
Datalines;
uncouple between ETTX & TTGX
TTPX and ETTX uncoupled
separation between DTTX and
ETTX and ETTX
;
Run;

data work.blah;
set work.data;
blah = find(A,"ETTX" );
blah2 = find(A,"ETTX",blah+1);
car1 = substr(A,blah,4);
car2 = substr(A,blah2,4);
run;

here is the output:

 

 

blah4.JPG

 

what is the easiest way to have SAS scan a 'list' of characters to search for?

 

for example, I not only want to search for ETTX, but DTTX, TTPX, TTGX, as well as others that aren't in this example.  is there a way for SAS to scan for the first instance of these characters and output that, and then scan again and output the second occurance? 

 

this is what i need the output to look like:

blah5.JPG

and you can see the order of car1 and car2 outputs have changed too.

3 REPLIES 3
ballardw
Super User

Is there a fixed known maximum number of matches that you will find?

This does the find and assign to the "car" variable. Changes would be to have the number of elements in the F array match the number of items you are going to search for, make sure the length of the F array elements is large enough (replace the 4) and have the number of items in the temporary list match the declared number.

The current comparsion is on equality, you may need to UPCASE the TEMP value, the F[j] value or both for the comparison.

 

data work.blah;
   set work.data;
   array f (5) $ 4 _temporary_ ('ETTX','TTGX','TTPX','DTTX','PPPQ');
   array result {10} $ 4 Car1-Car10;
   length temp $ 20.;
   Pos=0;
   Do i = 1 to (countw(A));
      temp = scan(a,i);
      Do j= 1 to dim(f);
         if f[j] = temp then do;
            pos = pos + 1;
            result[pos] = f[j];
         end;
      end;
   end;
   drop pos i j temp;
run;
Astounding
PROC Star

The easiest way is probably to switch the search around.  Take a word at a time from A, and see if it matches any of the values you are searching for ... something along these lines:

 

do i=1 to countw(A);

nextword = scan(A, i, ' ');

if nextword in ('ETTX', 'DTTX', etc.) then do;

 

Ksharp
Super User
Data have;
length A $40;
infile datalines dsd;
Input A $;
Datalines;
uncouple between ETTX & TTGX
TTPX and ETTX uncoupled
separation between DTTX and
ETTX and ETTX
;
Run;
data want;
 set have;
 pid=prxparse('/ETTX|TTGX|TTPX|DTTX/o');
 s=1;
 e=length(a);
 call prxnext(pid,s,e,a,p,l);
 do while(p>0);
  found=substr(a,p,l);
  output;
  call prxnext(pid,s,e,a,p,l);
 end;
 drop s e p l pid;
run;

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
  • 912 views
  • 0 likes
  • 4 in conversation