dear all
i have to create dummy variables based on the nic_code of the companies.
there are nearly 70 different nic_codes (which are not in serial order) for more than 5000 companies in my data set. So, i cannot do it manually in SAS.
if there is any shortcut programming to do this, please let me know.
the sample of my dataset format is as follows
Company Name | nic-code |
20 Microns Ltd. | 8 |
3I Infotech Ltd. | 62 |
3M India Ltd. | 22 |
3P Land Holdings Ltd. | 64 |
3Rd Rock Multimedia Ltd. | 62 |
52 Weeks Entertainment Ltd. | 59 |
5Paisa Capital Ltd. | 66 |
63 Moons Technologies Ltd. | 46 |
7Nr Retail Ltd. | 47 |
7Seas Entertainment Ltd. | 62 |
8K Miles Software Services Ltd. | 62 |
A & M Febcon Ltd. | 46 |
A & M Jumbo Bags Ltd. | 22 |
i need the output in the following format
please note that nic_coes are not in serial order from 1 to 100 and also all the codes from 1 to 100 are not present. .
Company Name | nic-code | nic_dummy01 | nic_dummy02 | nic_dummy03 | ………….. | nic_dummy100 |
20 Microns Ltd. | 8 | |||||
3I Infotech Ltd. | 62 | |||||
3M India Ltd. | 22 | |||||
3P Land Holdings Ltd. | 64 | |||||
3Rd Rock Multimedia Ltd. | 62 | |||||
52 Weeks Entertainment Ltd. | 59 | |||||
5Paisa Capital Ltd. | 66 | |||||
63 Moons Technologies Ltd. | 46 | |||||
7Nr Retail Ltd. | 47 | |||||
7Seas Entertainment Ltd. | 62 | |||||
8K Miles Software Services Ltd. | 62 | |||||
A & M Febcon Ltd. | 46 | |||||
A & M Jumbo Bags Ltd. | 22 | |||||
A 2 Z Infra Engg. Ltd. | 71 | |||||
A A R Commercial Co. Ltd. | 64 | |||||
A A R V Infratel Ltd. | 46 |
|
data have; set sashelp.class(keep=name); call streaminit(12345678); code=ceil(rand('uniform')*1000); run; data temp; set have; dummy=1; run; proc logistic data=temp outdesign=want outdesignonly noprint; class code/param=glm; model dummy=code/nofit noint; run; data final_want; merge have want(drop=dummy); run;
That's a very simple application of an array:
data want;
set have;
array nic_dummy {100} nic_dummy001-nic_dummy100;
nic_dummy{nic_code} = 1;
run;
So there are many SAS procedures that will compute the dummy variables for you, so you don't have to do it yourself. And thus, before you go about creating dummies, you would be wise to explain further what you are planning to do with this data.
data have; set sashelp.class(keep=name); call streaminit(12345678); code=ceil(rand('uniform')*1000); run; data temp; set have; dummy=1; run; proc logistic data=temp outdesign=want outdesignonly noprint; class code/param=glm; model dummy=code/nofit noint; run; data final_want; merge have want(drop=dummy); run;
data have;
set have;
*Company Name nic-code;
Dummy=1;
run;
proc sort data=have; by company_name; run;
proc transpose data=have out=want prefix=Dummy;
by company_name;
id nic_code;
var dummy;
run;
data want;
set have;
array nic_dummy {100} nic_dummy001-nic_dummy100;
do i = 1 to 100;
nic_dummy{i} = 0;
end;
nic_dummy{nic_code} = 1;
drop i;
run;
@srikanthyadav44 wrote:
thank you dear smantha for your valuable replay.
i am getting the output with dummy variables. but the missing values in the dummy variable are not filled with zero. all the missing values are blank.
I have to fill the missing values with zero. As there are large number of dummies, i cannot do it manual in SAS.
please suggest a code which can create dummies with 1 and 0 values, not just '1'.
thanks in advance
You could let many SAS PROCs generate the dummy variables for you, and then you don't have to do it yourself and potentially get it wrong.
@PaigeMiller, Reading through this thread, you replied twice about "letting many SAS procs generate the dummy variables for you" yet you never suggested what SAS procs to use or gave an example. When people post a question here, they obviously don't know how to do it and are asking for help. Simply stating that SAS can do it for you is not helpful in the least. Notice how the other two that replied gave sample code. It may not be the most efficient but one of them apparently worked for @srikanthyadav44 . You might as well have just replied..."go read the manual". Sorry, but replies like yours just waste time.
If you had taken the time to actually read @PaigeMiller 's posts, you would have found this.
you would be wise to explain further what you are planning to do with this data.
@srikanthyadav44 never provided the details, so there is no way to know which procedure would get the final goal.
@brian20435 wrote:
@PaigeMiller, Reading through this thread, you replied twice about "letting many SAS procs generate the dummy variables for you" yet you never suggested what SAS procs to use or gave an example. When people post a question here, they obviously don't know how to do it and are asking for help. Simply stating that SAS can do it for you is not helpful in the least. Notice how the other two that replied gave sample code. It may not be the most efficient but one of them apparently worked for @srikanthyadav44 . You might as well have just replied..."go read the manual". Sorry, but replies like yours just waste time.
And I could send @srikanthyadav44 on a wild goose chase that will not help him further by suggesting PROC ABCDEF to create dummy variables, when that's not what he needs. Or I could name all the PROCs that will do this and let @srikanthyadav44 try each one to find the one that does what he wants. At no time did I say "read the manual". I said: explain what you need.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.