Hello,
The ICDGRP variable has 52 values. I'm trying to create a dummy variable for each as:
If ICDGRP =1 then ICDGRP1=1; else ICDGRP1 =0;
If ICDGRP =2 then ICDGRP2 =1; else ICDGRP2 =0;
.
.
If ICDGRP =52 then ICDGRP52 =1; else ICDGRP52 =0;
But the SAS code below gives me the error of "The variable ICDGRP has been already defined" under the first ICDGRP - here ICDGRP(52).
Thank you.
data mydata1;
set mydata;
array ICDGRP(52) ICDGRP1-ICDGRP52;
do i = 1 to 52;
If ICDGRP(i) = (i) then ICDGRP(i) = 1; else ICDGRP(i)=0;
end;
run;
: "The variable ICDGRP has been already defined"
You cannot have an array name that is the same as a variable name. Obviously there is a variable named ICDGRP in data set MYDATA.
Which brings us to a bigger point. Why create all these dummy variables in the first place? Dummy variables are rarely needed in SAS, because most SAS analysis procedures can easily create their own dummy variables behind the scenes without you having to write program code to create the dummy variables.
So what is the purpose of creating dummy variables here? What analyses/reports/tables would you create once you have these dummy variables?
You cannot have an array name that is the same as a variable name. Obviously there is a variable named ICDGRP in data set MYDATA.
Which brings us to a bigger point. Why create all these dummy variables in the first place? Dummy variables are rarely needed in SAS, because most SAS analysis procedures can easily create their own dummy variables behind the scenes without you having to write program code to create the dummy variables.
So what is the purpose of creating dummy variables here? What analyses/reports/tables would you create once you have these dummy variables?
Thank you for the reply.
I'm estimating a ratio classified by ICDGRP in SUDAAN (Proc ratio). For that, I need to create dummy variable for each value of ICDGRP.
I need to create dummy variable for each value of ICDGRP.
That would mean this code:
data mydata1;
set mydata;
array _ICDGRP(52) ICDGRP1-ICDGRP52;
do I = 1 to 52;
if ICDGRP = I then _ICDGRP(I) = 1; else _ICDGRP(I)=0;
...
end;
run;
Note the array name. It is different from the variable name you already have.
Also note that the test:
if ICDGRP = I then _ICDGRP(I) = 1; else _ICDGRP(I)=0;
can be written as:
_ICDGRP(I) = (ICDGRP = I) ;
Edited: Added missing underscores in test statement.
I think you mean
if ICDGRP = I then _ICDGRP(I) = 1; else _ICDGRP(I)=0;
note the underscores in the array name
Use GLMSELECT to generate the dummy variables.
https://blogs.sas.com/content/iml/2020/08/31/best-generate-dummy-variables-sas.html
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 16. 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.