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
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.
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;
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;
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
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.
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.