BookmarkSubscribeRSS Feed
Obsidian | Level 7



Can anyone help me w/ the issue I am having with if-then-do statement. I am having problem defining med (please see underlined). For instance if I'd like to choose the med that meets any of  the following codes in addition to '45.40', how should go about it ?? :  '27.40' '87.4' '69.4' and '69.5' 


data a;

label med='medication'
prxdelv = 'delivery';
retain med
if _n_=1 then

med = '45.40' 
prxdelv = prxparse("/\b(6[3-7][0-9])\b|\b(6[3-7][0-8][0-9])\b|\b(679[0-1])\b|\b(6[3-7][0-8][0-9][0-9])\b|\b(6790[0-9])\b|\b(6791[0-4])\b/");


Thank you in advance for your time!!! 

Super User

You don't say what your code variable may be but if there is a relatively small list of codes you are interested in then the easiest is;


Data a;

   set have; /* this would be a data set that has code values*/

   if code in ('45.40' '27.40' '69.4' '69.5') then Med = (whatever indicates it is in that list)



OR if you are attempted to create a data set with med having those values:


data a;

   length med $ 5;

   do med = '45.40', '27.40', '69.4', '69.5' ;







Obsidian | Level 7

Hello Ballardw. 

Thank you for the quick response. I tried what you suggested (do med = '45.40', '27.40', '69.4', '69.5'), but the error message said "Expecting an arithmetic operator" "The symbol is not recognized and will be ignored". 


Sorry, if I wasn't clear before, but I am basically trying to identify people who met the following criteria: 1) med = '45.40', '27.40', '69.4', '69.5' and 2) prxdelv between 630-6794. I used prxparse for the second one, but having problem w/ the first one. It doesn't make sense to me to use prxdelv here. If it is just one condition, I would use something like: med= '45.40' , right? Hope this makes sense! 


Thanks again!



Super User

Please post the log where you had the error with the do med= approach. I do not get an error.


Your trial code did not reference any data set or any variables other than the ones shown. It would help to post a data step that would create a few observastions and then the desired output.

If you want to search for a variable having a number of values then the operator you want is IN

But I cannot tell if you have an existing variable MED or not. If you do then maybe you want

IF MED in ('45.40' '27.40' '69.4' '69.5') then <do something but I can't tell what>;


Your example code


med = '45.40' 
prxdelv = prxparse("/\b(6[3-7][0-9])\b|\b(6[3-7][0-8][0-9])\​b|\b(679[0-1])\b|\b(6[3-7][0-8][0-9][0-9])\b|\b(67​90[0-9])\b|\b(6791[0-4])\b/");

The med='45.40' attempts to assign a value to MED but the statment is missing a semicolon so would not execute.


Obsidian | Level 7

Hi there 

Please see below. I've underlined the part where I am having the issue. Here's the error message I am getting: 


ERROR 388-185: Expecting an arithmetic operator.

ERROR 200-322: The symbol is not recognized and will be ignored.




data try_1;


        label  med=’Medication'

               prxdelv =’Delivery’;

       retain  med


if _n_=1 then



med    =’4540’, 2740’, ‘6940’, ‘6950’;

prxdelv = prxparse("/\b(6[3-7][0-9])\b|\b(6[3-7][0-8][0-9])\b|\b(679[0-1])\b|\b(6[3-7][0-8][0-9][0-9])\b|\b(6790[0-9])\b|\b(6791[0-4])\b/");



     label medrec   = 'Record with Med Code (Y=1 / N=0)'

           delvrec   = 'Record with Delivery Code (Y=1 / N=0)'

           countrecs = 'Count of Records with Both Delivery and Med Codes';


                  pdx_delv $200

                  ppx_med $200;

     retain countrecs  0;

     format countrecs comma8.;

     set try;

                end = lastrec;

     pdx_delv  = catx(' ', PDX, SDX1, SDX2, SDX3, SDX4, SDX5, SDX6, SDX7,    

                 SDX8, SDX9, SDX10);

     ppx_med = catx(' ', PPX, SPX2, SPX3);

     medrec    = ( 1 <= index(ppx_med, med) );

     delvrec    = ( 1 <= prxmatch(prxdelv, pdx_delv) );

     if medrec + delvrec = 2 then

           countrecs = 1 + countrecs;


     if lastrec then                              

           put 'Records with both codes: ' countrecs;




Super User

Please clarify:

Are you attempting to assign a value to med with this:

med    =’4540’, 2740’, ‘6940’, ‘6950’;?

If so that is incorrect syntax.


WHAT is the variable the currently has the value that you are going to compare with med? Your code references variables such as

PDX, SDX1, SDX2, SDX3, SDX4, SDX5, SDX6, SDX7,  

but there is nowhere for them to come from since the is NO Set statement to indicate that you are using a dataset.


I think we are at a point where you need to provide some example starting data and the desired result for a few cases as I cannot follow what you are actually apparently attempting.



  This is an invalid assignment statement:

med    =’4540’, '2740’, ‘6940’, ‘6950’;


You can't have a comma in an assignment statement unless it is part of a function call, like:

newvar = sum(oldvar1, oldvar2, oldvar3);


  Rick's suggestion to do your test with an IN operator is a good suggestion.


  If, on the other hand, you were trying to create a character variable named to hold a list of character values, then you might do something like this: 

med="4540, 2740, 6940, 6950";


  Although I'm not sure how you would use the MED variable in a program. 


  I'm still not entirely clear on what your data looks like and what your end result is supposed to be. Here's what I imagine you have for data from some of what you describe:

name    pdx    sdx1     sdx2    sdx3     ppx    spx1    spx2
anna    110    111      112      113     4540   1111    6940
bob     113    114      115      116     2222   4540    3333

  Based on a structure like this -- where every observation has multiple "dx" variables and multiple "px" variables, then it seems to me that you want to possibly use array processing. I am not clear which variables are character and which might be numeric or even if the structure of your data is anything at all like this. For me, it is hard to write a program without being able to understand the data that is coming into the program


  I am not sure what you are trying to accomplish with the PRXPARSE or the concatenation of the variables, except maybe make one long text string from all the variables or some of the variables on an observation in order to bypass having multiple conditions in an IF statement.


  My recommendation is to simplify your program, get 1 assignment/test/condition working and then introduce more elements.





It looks to me like you have numerical variables and the problem is that you are trying to find them by using character variables.  You said "I am basically trying to identify people who met the following criteria: 1) med = '45.40', '27.40', '69.4', '69.5' and 2) prxdelv between 630-6794."

If those variables are numerical, you can say


if med in (45.40, 27.40, 69.4, 69.5) and 

   (630 <= prxdelv <= 6794) then 



For example, here is how to find certain students who meaet age and height requirements:


data class;
set sashelp.class;
if age in (10, 12 16) and 
   (50 <= height <= 100) then 
   found = 1;

proc print; run;


Super User Tom
Super User

Sounds like you want to identify patients are taking a particular MEDICATION and also have received a particular type of PROCEDURE.


So first you need to know what variables contain the medication codes and what medication codes you want to search for. From your other message it looks like the variables are PPX, SPX2, SPX3 and the values are '45.40', '27.40', '69.4', '69.5'.  You can do this will a little "wallpaper" code.

med_received = in(PPX,'45.40', '27.40', '69.4', '69.5')
  or  in(SPX2,'45.40', '27.40', '69.4', '69.5')
  or  in(SPX3,'45.40', '27.40', '69.4', '69.5')


And second you need to know what variables contain the procedure codes and what procedure codes you want to search for.  For this it looks like the variables are PDX, SDX1, SDX2, SDX3, SDX4, SDX5, SDX6, SDX7, SDX8, SDX9, SDX10 and the values are between '630' and '6794'.  Again you could do this with some wallpaper code:


proc_received = ('630' <= PDX <=  '6794~')
  or ('630' <= SDX1 <=  '6794~')


Then you need know how to find each and also how to insure that an individual is in both groups.  This is pretty easy if there is just one record per individual.  But if you want to check across observations (say the medication is recorded on a different observation than where the procedure is recorded) then you need a method to collapse to a single record.  A useful technique for this is to use a "DOW" loop to collapse to just one record per group.


data want ;
  do until ( ;
    set have ;
    by pid ;
    ** Calculate MED_RECEIVED per above **;
    ** Calculate PROC_RECEIVED per above  **;
    * Now collapse these ;
    if med_received then any_med=1;
    if proc_received then any_proc=1;
  if any_med AND any_proc then output;



Obsidian | Level 7

Thanks, everyone!! It seems like I caused a considerable amount of confusion over what I was trying to achieve as well as the structure of my dataset. Tom: thanks for the suggestion! I think I am going to try what you suggested below. The only problem is with the volumn of recrods to be searched though- I hope the operation doesn't exceed the available memory. Cynthia: It worked for me to concatenate all the medications, then searched the record once. With the IN syntax I still have multiple searches- one for each alternative. Wouldn't the regex avoid that? 




Again thank you so much for all of your help! 




Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Get the $99 certification deal.jpg



Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 9 replies
  • 5 in conversation