I aim to create a SAS data set from a PROC HTTP-generated output text file using an IF-THEN DELETE statement. But first, I want to create a PATTERN variable using the PRXMATCH match function using the variable STRING (Please see the code in the DATA step below).
I desire SAS to return a non-zero value for the PATTERN variable if the STRING contains text like “HC-226, “HC-085A, or “HC-028 provided the STRING value does not contain Replaced or CD-ROM. Otherwise, I want SAS to return a zero for the PATTERN variable. However, if the STRING value contains text like Replaced or CD-ROM, I want a zero for the PARREN variable.
My first attempt seems to work if the STRING contains the text like the following: “HC-226, “HC-085A, “HC-028 … and the STRING value does not contain Replaced or CD-ROM.
position=prxmatch('m/"HC-\d{3,}/i',string);
Issue: I don’t know how to modify the above code to get a zero for the PARREN variable if the STRING value contains text like Replaced or CD-ROM.
data _null_; input string $char120.; position=prxmatch('m/"HC-\d{3,}/i',string); put (_ALL_) (=/); datalines; <option value="HC-226">MEPS HC-226: MEPS Panel 23 Three-Year Longitudinal Data File</option> <option value="HC-220H">MEPS HC-220H: 2020 Home Health File</option> <option value="HC-093">HC-093 2006 P10R3/P11R1 Population Characteristics</option> <option value="HC-085A">HC-085A 2004 Prescribed Medicines</option> <option value="HC-028">HC-021 1998 Full Year Population Characteristics (HC-021 replaced by HC-028)</option> <option value="HC-014">HC-014 1996 MEPS HC Survey Data (CD-ROM)</option> <option value="LINK_98HC/IC">HC-IC Linked Data, 1998</option> ; run;
434 data _null_; 435 input string $char120.; 436 position=prxmatch('m/"HC-\d{3,}/i',string); 437 put (_ALL_) (=/); 438 datalines; string=<option value="HC-226">MEPS HC-226: MEPS Panel 23 Three-Year Longitudinal Data File</option > position=15 string=<option value="HC-220H">MEPS HC-220H: 2020 Home Health File</option> position=15 string=<option value="HC-093">HC-093 2006 P10R3/P11R1 Population Characteristics</option> position=15 string=<option value="HC-085A">HC-085A 2004 Prescribed Medicines</option> position=15 string=<option value="HC-028">HC-021 1998 Full Year Population Characteristics (HC-021 replaced by HC-028)</option> position=15 string=<option value="HC-014">HC-014 1996 MEPS HC Survey Data (CD-ROM)</option> position=15 string=<option value="LINK_98HC/IC">HC-IC Linked Data, 1998</option> position=0 NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.00 seconds 446 ; 447 run;
Any help will be appreciated. Thanks,
Please make your question and description match your code, or vice versa. You repeatedly say PARREN (at maybe PATERN) but there is no variable in the code with that name but it appears you are looking for POSITION.
One way is to use an additional function such as FIND to see if your other words are present and then reset the POSITION variable to 0. Default behavior with FIND is it returns the character position number of the target string, such as your "replaced". So if the value is > 0 it was found. The 'i' argument makes the search case insensitive.
data _null_; input string $char120.; position=prxmatch('m/"HC-\d{3,}/i',string); if find(string,'replaced','i')>0 or find(string,'cd-rom','i')>0 then position=0; put (_ALL_) (=/); datalines; <option value="HC-226">MEPS HC-226: MEPS Panel 23 Three-Year Longitudinal Data File</option> <option value="HC-220H">MEPS HC-220H: 2020 Home Health File</option> <option value="HC-093">HC-093 2006 P10R3/P11R1 Population Characteristics</option> <option value="HC-085A">HC-085A 2004 Prescribed Medicines</option> <option value="HC-028">HC-021 1998 Full Year Population Characteristics (HC-021 replaced by HC-028)</option> <option value="HC-014">HC-014 1996 MEPS HC Survey Data (CD-ROM)</option> <option value="LINK_98HC/IC">HC-IC Linked Data, 1998</option> ; run;
There may be a PRX method to accomplish this but not my strong point.
Please make your question and description match your code, or vice versa. You repeatedly say PARREN (at maybe PATERN) but there is no variable in the code with that name but it appears you are looking for POSITION.
One way is to use an additional function such as FIND to see if your other words are present and then reset the POSITION variable to 0. Default behavior with FIND is it returns the character position number of the target string, such as your "replaced". So if the value is > 0 it was found. The 'i' argument makes the search case insensitive.
data _null_; input string $char120.; position=prxmatch('m/"HC-\d{3,}/i',string); if find(string,'replaced','i')>0 or find(string,'cd-rom','i')>0 then position=0; put (_ALL_) (=/); datalines; <option value="HC-226">MEPS HC-226: MEPS Panel 23 Three-Year Longitudinal Data File</option> <option value="HC-220H">MEPS HC-220H: 2020 Home Health File</option> <option value="HC-093">HC-093 2006 P10R3/P11R1 Population Characteristics</option> <option value="HC-085A">HC-085A 2004 Prescribed Medicines</option> <option value="HC-028">HC-021 1998 Full Year Population Characteristics (HC-021 replaced by HC-028)</option> <option value="HC-014">HC-014 1996 MEPS HC Survey Data (CD-ROM)</option> <option value="LINK_98HC/IC">HC-IC Linked Data, 1998</option> ; run;
There may be a PRX method to accomplish this but not my strong point.
Hi ballardw,
Thank you for revising my code. Your revised code provides me with desired results.
data _null_; input string $char120.; position=prxmatch('m/"HC-\d{3,}/i',string); if find(string,'replaced','i')>0 or find(string,'cd-rom','i')>0 then position=0; put (_ALL_) (=/); datalines;
Hi ballardw (Super User),
Thanks for your code. Sorry for the typos. I have corrected PARREN and PATERN as POSITION.
I aim to create a SAS data set from a PROC HTTP-generated output text file using an IF-THEN DELETE statement. But first, I want to create a POSITION variable using the PRXMATCH function using the variable STRING (Please see the code in the DATA step below).
I desire SAS to return a non-zero value for the POSITION variable if the STRING contains text like “HC-226, “HC-085A, or “HC-028 provided the STRING value does not contain Replaced or CD-ROM. Otherwise, I want SAS to return a zero for the POSITION variable. However, if the STRING value contains text like Replaced or CD-ROM, I want a zero for the POSITION variable.
My first attempt seems to work if the STRING contains the text like the following: “HC-226, “HC-085A, “HC-028 … and the STRING value does not contain Replaced or CD-ROM.
position=prxmatch('m/"HC-\d{3,}/i',string);
Issue: I don’t know how to modify the above code to get a zero for the POSITION variable if the STRING value contains text like Replaced or CD-ROM.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.