BookmarkSubscribeRSS Feed
SivaKizildag
Fluorite | Level 6

Hello,

 

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 in.data;

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

run;

 

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 in.data;

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' ;

run;

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, 

2 REPLIES 2
tomrvincent
Rhodochrosite | Level 12
Simply create a 2nd field that contains the 2nd grouping.
ed_sas_member
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 in.data;

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' ;

run;

Best,

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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
  • 683 views
  • 1 like
  • 3 in conversation