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:
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:
and you can see the order of car1 and car2 outputs have changed too.
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;
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;
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;
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: