SAS Programming

DATA Step, Macro, Functions and more
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')
;

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

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

Browse our catalog!

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