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

I am trying to create 4 new variables(var_A, Var_B, Var_C, Var_D).  Where Var_A has a value of 1 if Response is A, and a value of 0 otherwise.  And Var_B has a value of 1 if Response is B , and a value of 0 otherwise.  Likewise for C and D.  

 

I have done it the following 2 ways, and am wondering if there is a built in function for such a process.  

 

** Way 1:

 

Data Table1;
     set Table1;

          if Response = "A" then
               Var_A = 1;
          else if Response = "B" then
               Var_B = 1;
          else if Response = "C" then
               Var_C = 1;
          else if Response = "D" then
               Var_D = 1;
run;

 

/*macros to add zeros to the non 1 places from above*/
%LET List= A B C D;

%Macro Zeros();

          data Table1;
          set Table1;

               if Var_&MM. ^= 1 then
                    Var_&MM. = 0;
run;

%MEND Zeros;

 

%Macro Loop_M();
     %DO M=1 %TO 4;
     %LET MM=%SCAN(&List., &M.);

     %Zeros();
     %END;
%MEND Loop_M;

 

%Var_M();

 

 

** Way 2:

 

Data Table1;
     set Table;

         if Response = "A" then do
              Var_A=1; Var_B=0; Var_C=0; Var_D=0;
        end;
        else if Response = "B" then do
              Var_B=1; Var_A=0; Var_C=0; Var_D=0;
        end;
        else if Response = "C" then do
              Var_C=1; Var_A=0; Var_B=0; Var_D=0;
        end;
        else if Response = "D" then do
              Var_D=1; Var_A=0; Var_B=0; Var_C=0;
        end;
        else do
              Var_A=0; Var_B=0; Var_C=0; Var_D=0;
        end;
run;

 

Is there an easier way to accomplish this?  

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
smantha
Lapis Lazuli | Level 10
Data Table1;
     set Table;
          Var_A=0; Var_B=0; Var_C=0; Var_D=0;
         if Response = "A" then do
              Var_A=1;
        end;
        else if Response = "B" then do
              Var_B=1;
        end;
        else if Response = "C" then do
              Var_C=1; 
        end;
        else if Response = "D" then do
              Var_D=1; 
        end;
       
run;

Data Table1;
     set Table;
          
        Var_A = 1* (Response='A');
         Var_B = 1* (Response='B');
         Var_C = 1* (Response='C');
           Var_D = 1* (Response='D');
     
        
       
run;

Data Table1;
     set Table;
        %let i = 1 %to 4;
            %let varlst = A B C D;
            %let var=%scan(&varlst.,&i.);
              Var_&var.= 1* (Response="&var."); 
        %end;  
             
run;

Three ways hown above

View solution in original post

7 REPLIES 7
smantha
Lapis Lazuli | Level 10
Data Table1;
     set Table;
          Var_A=0; Var_B=0; Var_C=0; Var_D=0;
         if Response = "A" then do
              Var_A=1;
        end;
        else if Response = "B" then do
              Var_B=1;
        end;
        else if Response = "C" then do
              Var_C=1; 
        end;
        else if Response = "D" then do
              Var_D=1; 
        end;
       
run;

Data Table1;
     set Table;
          
        Var_A = 1* (Response='A');
         Var_B = 1* (Response='B');
         Var_C = 1* (Response='C');
           Var_D = 1* (Response='D');
     
        
       
run;

Data Table1;
     set Table;
        %let i = 1 %to 4;
            %let varlst = A B C D;
            %let var=%scan(&varlst.,&i.);
              Var_&var.= 1* (Response="&var."); 
        %end;  
             
run;

Three ways hown above

mcook
Quartz | Level 8
I really liked that 2nd way. I never thought of doing it like that.
s_lassen
Meteorite | Level 14

It could be done with arrays, e.g.:

data want;
  set have;
  array responses(4) $1 _temporary_ ('A','B','C','D');
  array vars(4) 8 Var_A Var_B Var_C Var_D;
  do _N_=1 to dim(vars);
    vars(_N_)=(Response=responses(_N_));
    end;
run;
PaigeMiller
Diamond | Level 26

SAS has built-in ways to create 0/1 (sometimes called "dummy" variables) so you don't have to program it yourself (and risk getting it wrong). One method is PROC GLMMOD, but there are many other methods as discussed in this thread How to create dummy variables - Categorical Variables

 

In addition, many SAS procedures allow you to use a CLASS statement, so the 0/1 variables are created by the PROC internally, so you don't have to create them at all. Please consider this approach, as it is the least painful and least error-prone method.

 

But I guess the real question is, what are you going to do with these 0/1 variables once you create them?

--
Paige Miller
smantha
Lapis Lazuli | Level 10

I believe you didn't intent to use _n_;

ballardw
Super User

A better question may be "Why do you need 4 variables?".

One of the typical uses of what you are requesting is to use the 1/0 coded values as "dummy" or "indicator" variables in regression analysis. Most of the SAS regression procedures will allow you to place your Response variable on a CLASS statement so that when it is used in the Model the levels are created as needed.

 

And another way

proc format;
invalue AA
'A'=1
other=0
;
invalue BB
'B'=1
other=0
;
invalue CC
'C'=1
other=0
;
invalue DD
'D'=1
other=0
;
run;

data example;
   input response $;
   a = input(response,aa.);
   b = input(response,bb.);
   c = input(response,cc.);
   d = input(response,dd.);
datalines;
A
D
B
C
;

The informats have a 2 character name because a single D will conflict with a SAS supplied informat, or at least that was the error I got. So make them all similar in name with 2 characters.

Ksharp
Super User
data have;
input response $;
cards;
A
B
C
D
;
data temp(rename=(response=var_));
 set have;
 y=1;
run;
proc logistic data=temp outdesign=design(drop=y) outdesignonly;
class var_ / param=glm ;
model y=var_/nofit noint;
run;
data want;
 merge have design;
run;

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
  • 7 replies
  • 677 views
  • 2 likes
  • 6 in conversation