- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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"
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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?
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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?
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I think you mean
if ICDGRP = I then _ICDGRP(I) = 1; else _ICDGRP(I)=0;
note the underscores in the array name
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Use GLMSELECT to generate the dummy variables.
https://blogs.sas.com/content/iml/2020/08/31/best-generate-dummy-variables-sas.html