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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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.

 

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

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.

 

Astounding
PROC Star

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.

Reeza
Super User
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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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