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

Using SAS 9.4

 

I have the following data:

acl_reconacl_repairpcl_reconpcl_repairmcl_reconmcl_repairlcl_reconlcl_repairplc_recon

plc_repair

 

All of the above are binary (Yes/No) variables and none have missing data. Is there an efficient way to code looking for where a "Yes" occurs within these variables and outputting where multiple "Yes" occur? Meaning, If "acl_recon" and "lcl_recon" and "mcl_repair" and "plc_recon" are all = 'Yes' the output variable (I would like all of the data to go into 1 variable) should read "ACL recon/LCL recon/MCL repair/PLC recon". It is possible there could only be 1 variable with a "Yes" or there could be 9 variables with a "Yes". Any thoughts would be helpful. Thank you

 

Is it possible to do this without having to write lots of if/then statements? Thank you

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

If I understand you correctly, do this

 

data have;
input (acl_recon acl_repair pcl_recon pcl_repair mcl_recon mcl_repair lcl_recon lcl_repair plc_recon plc_repair)(:$);
datalines;
Yes No Yes Yes No Yes No Yes Yes Yes
Yes No Yes No No Yes No Yes Yes Yes
Yes No No Yes No Yes No No Yes Yes
;

data want(drop=i);
    set have;
    length newvar $ 200;
    array a _character_;
    do i = 1 to dim(a);
        if a[i] = 'Yes' then do;
            newvar = catx('/', newvar, vname(a[i]));
        end;
    end;
run;

View solution in original post

6 REPLIES 6
PeterClemmensen
Tourmaline | Level 20

If I understand you correctly, do this

 

data have;
input (acl_recon acl_repair pcl_recon pcl_repair mcl_recon mcl_repair lcl_recon lcl_repair plc_recon plc_repair)(:$);
datalines;
Yes No Yes Yes No Yes No Yes Yes Yes
Yes No Yes No No Yes No Yes Yes Yes
Yes No No Yes No Yes No No Yes Yes
;

data want(drop=i);
    set have;
    length newvar $ 200;
    array a _character_;
    do i = 1 to dim(a);
        if a[i] = 'Yes' then do;
            newvar = catx('/', newvar, vname(a[i]));
        end;
    end;
run;
GS2
Obsidian | Level 7 GS2
Obsidian | Level 7
Thank you! That worked perfectly
ballardw
Super User

Just for giggles, what is that variable with variable names to be used for?

 

And with what tools?

GS2
Obsidian | Level 7 GS2
Obsidian | Level 7

It is being used for a descriptive gut check basically

Tom
Super User Tom
Super User

If your flags were actual binary numeric variables you could use SUM() function.

proc print data=have;
where 1<sum(acl_recon,acl_repair,pcl_recon,pcl_repair,mcl_recon,mcl_repair 
           ,lcl_recon,cl_repair,plc_recon,plc_repair)
;
run;

With character variables you can use COUNT() and CATS().

where 1<count(cats(acl_recon,acl_repair,pcl_recon,pcl_repair,mcl_recon,mcl_repair 
                  ,lcl_recon,cl_repair,plc_recon,plc_repair)
             ,'Yes')
;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1569 views
  • 2 likes
  • 4 in conversation