BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
rakeshvvv
Quartz | Level 8

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

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

5 REPLIES 5
ballardw
Super User

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;

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
rakeshvvv
Quartz | Level 8

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

 

Tom
Super User Tom
Super User

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.

 

ballardw
Super User

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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

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

View all other training opportunities.

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