DATA Step, Macro, Functions and more

sas query

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 145
Accepted Solution

sas query

Dear All

 

I have dataset with 7 variables. ID, FLAG1-FLAG5, FINAL FLAG..

 

The purpose of the query is to derive a final flag based on the values of the flag1-flag5. 

 

ID        FLAG1  FLAG2  FLAG3  FLAG4  FLAG5  FINAL_FLAG

ABC           1            1         0            1          0             1(FLAG1,FLAG2,FLAG4)

DEF           0            1         1            0          0             1(FLAG2,FLAG3)

XYZ           0            1         0            1          0             1(FLAG2,FLAG4)

PTR           0            0         0            0          0             0

 

Thanks in advance

 

Rakesh


Accepted Solutions
Solution
‎06-13-2016 03:58 PM
Super User
Super User
Posts: 7,079

Re: sas query

Posted in reply to rakeshvvv

Look at the definition of the ARRAY statement.

I think what you are asking is to change code like this:

ARRAY FLAG (5) ;

That uses variables named FLAG1 to FLAG5 in the array.

To code like this that uses the variables you specify in the array.

ARRAY FLAG
     CMTRT_Flag  CMDOSE_Flag  CMUNIT_Flag
     CMOTSP_Flag  CMROUTE_Flag  CMROSP_Flag 
     CMFREQ_Flag CMFOSP_Flag  CMONGO_Flag 
     CMMHIND_Flag CMAEIND_Flag
;

Note that you do not need to tell SAS how many variables are in the array.  It will count them for you.  

 

In your other code you can use the function call DIM(FLAG) to know how many variables are in the array.

 

View solution in original post


All Replies
Super User
Posts: 11,343

Re: sas query

Posted in reply to rakeshvvv

Is the rule that if any of the flag1 - flag5 is 1 then the result for final flag is 1 otherwise 0?

If so this may work for you

 

data want;

   set have;

   final_flag= max(of flag1-flag5);

run;

Super User
Super User
Posts: 7,997

Re: sas query

Posted in reply to rakeshvvv

Not tested (post test data in the form of a datastep if you want tested code) but:

data want;
  set have;
  array flag{5};
  do i=1 to 5;
    if flag{i} then final_flag=catx(final_flag,vname(flag{i});
  end;
run;
Frequent Contributor
Posts: 145

Re: sas query

Hi , looks like the below arry code will work but can you me out with below variables...they are not following pattern of flag1-flag5 and istead they have name in the flag.....can you help me out......

 

SUBJECT CMTRT_Flag  CMDOSE_Flag  CMUNIT_Flag CMOTSP_Flag  CMROUTE_Flag  CMROSP_Flag CMFREQ_Flag CMFOSP_Flag  CMONGO_Flag CMMHIND_Flag CMAEIND_Flag

 

Solution
‎06-13-2016 03:58 PM
Super User
Super User
Posts: 7,079

Re: sas query

Posted in reply to rakeshvvv

Look at the definition of the ARRAY statement.

I think what you are asking is to change code like this:

ARRAY FLAG (5) ;

That uses variables named FLAG1 to FLAG5 in the array.

To code like this that uses the variables you specify in the array.

ARRAY FLAG
     CMTRT_Flag  CMDOSE_Flag  CMUNIT_Flag
     CMOTSP_Flag  CMROUTE_Flag  CMROSP_Flag 
     CMFREQ_Flag CMFOSP_Flag  CMONGO_Flag 
     CMMHIND_Flag CMAEIND_Flag
;

Note that you do not need to tell SAS how many variables are in the array.  It will count them for you.  

 

In your other code you can use the function call DIM(FLAG) to know how many variables are in the array.

 

Super User
Posts: 11,343

Re: sas query

And if the rule is the "any of the values are 1" type the modification to my suggested code using an array is

 

Final_flag = max( of Flag(*));

if the array of flag variables is named Flag.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 259 views
  • 3 likes
  • 4 in conversation