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

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" 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

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
altadata1
Obsidian | Level 7

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. 

ChrisNZ
Tourmaline | Level 20

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.

PaigeMiller
Diamond | Level 26

@ChrisNZ 

 

I think you mean

 

if ICDGRP = I then _ICDGRP(I) = 1; else _ICDGRP(I)=0; 

 

 

note the underscores in the array name 

--
Paige Miller

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 6 replies
  • 555 views
  • 3 likes
  • 4 in conversation