DATA Step, Macro, Functions and more

SAS Scanning and Output

Reply
Contributor
Posts: 39

SAS Scanning and Output

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.

Super User
Posts: 11,343

Re: SAS Scanning and Output

[ Edited ]
Posted in reply to laneylaners

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;
Super User
Posts: 5,513

Re: SAS Scanning and Output

Posted in reply to laneylaners

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;

 

Super User
Posts: 10,041

Re: SAS Scanning and Output

Posted in reply to laneylaners
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;
Ask a Question
Discussion stats
  • 3 replies
  • 227 views
  • 0 likes
  • 4 in conversation