Help using Base SAS procedures

If Then statement question

Reply
Contributor
Posts: 27

If Then statement question

Hi, 

 

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
prxdelv;
if _n_=1 then
do;

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/");
end;

 

Thank you in advance for your time!!! 

Super User
Posts: 11,343

Re: If Then statement question

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)

run;

 

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' ;

      output;

   end;

run;

 

 

 

Contributor
Posts: 27

Re: If Then statement question

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
Posts: 11,343

Re: If Then statement question

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

do;

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/");
end;

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

 

Contributor
Posts: 27

Re: If Then statement question

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

               prxdelv;

if _n_=1 then

do;

 

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/");

           end;

 

     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';

     length  

                  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;

     run;

 

 

Super User
Posts: 11,343

Re: If Then statement question

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.

SAS Super FREQ
Posts: 8,868

Re: If Then statement question

[ Edited ]

Hi:

  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.

 

cynthia

 

SAS Super FREQ
Posts: 3,755

Re: If Then statement question

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 

   DO_WHATEVER;

 

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;
else
   found=0;
run;

proc print; run;

 

Super User
Super User
Posts: 7,074

Re: If Then statement question

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 (last.pid) ;
    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;
  end;
  if any_med AND any_proc then output;
run;

 

 

Contributor
Posts: 27

Re: If Then statement question

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! 

 

Ask a Question
Discussion stats
  • 9 replies
  • 510 views
  • 0 likes
  • 5 in conversation