turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- Base SAS

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-16-2016 10:16 AM

Hello

The below code will mark each zero across the variables V1 to V10 (Column) as missing value (.dot)

data credit;

set file;

array x(*) V1 -- V10;

do i=1 to dim(x);

if x(i)=0 then x(i)=.;

end;

run;

Let's say in regression analysis while creating dummy variables, X variable has 7 categories and I would like to create dummy variables for those 7 categories.

How can I write codes using array or macros to create dummy variables, Is it possible?

Need help.

Accepted Solutions

Solution

01-24-2016
03:24 AM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to KafeelBasha

01-16-2016 10:28 AM

You could also use an ARRAY to help with making dummy variables. So if X has 7 values (1 to 7) then this code will make variables DUMMY1 to DUMMY7 with 0/1 values.

```
data want ;
set have ;
array dummy (7);
do i=1 to dim(dummy);
dummy(i)=(x=i);
end;
run;
```

Note that if X is not simply the values 1 to 7 then it is a little harder. You should look at the GLMMOD procedure as a way to generate dummy variables.

All Replies

Solution

01-24-2016
03:24 AM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to KafeelBasha

01-16-2016 10:28 AM

You could also use an ARRAY to help with making dummy variables. So if X has 7 values (1 to 7) then this code will make variables DUMMY1 to DUMMY7 with 0/1 values.

```
data want ;
set have ;
array dummy (7);
do i=1 to dim(dummy);
dummy(i)=(x=i);
end;
run;
```

Note that if X is not simply the values 1 to 7 then it is a little harder. You should look at the GLMMOD procedure as a way to generate dummy variables.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to KafeelBasha

01-16-2016 12:18 PM

Assuming X is numeric, you don't need to know the number of values ahead of time. Here is the idea:

proc sql noprint;

select distinct X into : list_of_x separated by ' ' from have;

quit;

data want;

set have;

%do i=1 %to &sqlobs;

dummy&i = (x=%scan(&list_of_x, &i));

%end;

run;

Of course, you will need to define a macro to hold the logic, because of the %DO loop.

Good luck.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to KafeelBasha

01-16-2016 09:48 PM

Also, depending on the Proc SAS may calculate the dummy variables for you. The CLASS statement is the common method, make sure to understand which method of dummy variable is being used if you do use the Class statement. The default is GLM coding rather than reference coding.