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.
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;
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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.