BookmarkSubscribeRSS Feed
Fluorite | Level 6



I have a dataset, where each row contains information about firms. There is a variable about the sector of the company; sect. There are up to 700 sectors, so I need to group them, like:


Industry: mining, textiles industry, food industry... and so on. I do this with the following code:


data temp; set;

if sect in ('01' '02' '03' '04' '05' '06')    then my_sector='Industry' ;



The tricky part is ; I also want to give out details within each sector. For instance, I want to keep this whole "Industry" sector, but I also want to have subgroups within "Industry". But when I run the following code: 


data temp; set;

if sect in ('01' '02' '03' '04' '05' '06')    then my_sector='Industry' ;

if sect in ('01' '02')    then my_sector='Textile Industry' ;
if sect in ('03' '04')    then my_sector='Food Industry' ;
if sect in ('05')         then my_sector='Mining Industry' ;


The subgroups work just fine, but the global "Industry" sector only contains what is not contained in other subgroups (here the sector 06). So mu question is; how do I have the 3 subgroups but also an "Industry" sector that has all the 3 subgroups and the rest of the industry (from 01 to 06)?


Thank you very much, 

Rhodochrosite | Level 12
Simply create a 2nd field that contains the 2nd grouping.
Meteorite | Level 14

Hi @SivaKizildag 


You face this issue because the variable for grouping is the same name as variable for subgrouping. So if statements for subgrouping erase the first one for grouping. As only "06" is not referenced for subgrouping, the variable for grouping contains only this value (not erased).

You should create another variable for subgrouping. I also encourage you to use "else if" in your statements to have a more efficient code.

data temp; set;

if sect in ('01' '02' '03' '04' '05' '06')    then my_sector='Industry' ;

if		sect in ('01' '02')    then my_subsector='Textile Industry' ;
else if sect in ('03' '04')    then my_subsector='Food Industry' ;
else if sect in ('05')         then my_subsector='Mining Industry' ;



Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 1 like
  • 3 in conversation