BookmarkSubscribeRSS Feed
Dregerator
Obsidian | Level 7

Hi,

 

I have this string: 

 

REFMATS= [Type:ADRG, URL :, Version : , Status : , Date : Notes :,[Type:P21 Report, URL :, Version : , Status : , Date : Notes :,[Type:eSub Source Data Location, URL :, Version : , Status : , Date : Notes :,[Type:eSub Data Package Location, URL :, Version : , Status : , Date : Notes :

 

I am using "[" as a delimiter to segment this string into clauses

 

data work._ref_mat_info (label = "Reference Materials") ;
length clause $200 ;
set test;
where REFMATS ne "";
_i = 0 ;
do until(REFMATS = "") ;
_i + 1 ;
if _i >= 100 then stop ;
_pos = index(REFMATS,'[');
if _pos = 0 then do ;
clause = REFMATS ;
REFMATS = "" ;
end ;
else do ;
clause = substr(REFMATS,1,_pos);
REFMATS = substr(REFMATS,sum(_pos,1));
end ;
output;
end ;
run ;  

 

However, it keeps outputting the first "[" as an individual clause so it is not lining up properly with the REFMATS string on the output dataset. Could you advise how to modify the code to use the delimiter "[" but it doesn't get outputted as an observation so the REFMATS line up properly with the clause. 

 

 

2 REPLIES 2
Astounding
PROC Star

It would be straightforward to use the SCAN function for this job.  That's what it was built for, along these lines:

data work._ref_mat_info (label = "Reference Materials") ;
   length clause $200 ;
   set test;
   where REFMATS ne " ";
   do _n_=1 to 100 until (clause=' ');
      clause = scan(REFMATS, _n_, '[');
      if clause ne ' ' then output;   
   end;
run;
ballardw
Super User

If you use Countw(Refmat,'[') can tell exactly how many elements there are to parse.

If you don't want the "REFMATS=" as a clause start at the second element.

data example (label = "Reference Materials") ;
   length clause $200 ;
   refmats="REFMATS= [Type:ADRG, URL :, Version : , Status : , Date : Notes :,[Type:P21 Report, URL :, Version : , Status : , Date : Notes :,[Type:eSub Source Data Location, URL :, Version : , Status : , Date : Notes :,[Type:eSub Data Package Location, URL :, Version : , Status : , Date : Notes :";
   do _n_=2 to countw(refmats,'[');
      clause = scan(REFMATS, _n_, '[');
      if clause ne ' ' then output;   
   end;
run;

 

Ready to join fellow brilliant minds for the SAS Hackathon?

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!
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
  • 2 replies
  • 534 views
  • 0 likes
  • 3 in conversation