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

Hi,

Let me start off by saying that I'm very new to SAS so excuse me for any minor mistakes.

I have data like this:

Region

Year

Month

Sales

North

2014

May

245

North

2014

May

347

North

2014

June

127

North

2014

May

645

South

2013

May

367

 and I need to define a unique interger value for each distinct evaluated value of (region&year&month).

 

Data want :

Region

Year

Month

Sales

RYMkey

North

2014

May

245

1

North

2014

May

347

1

North

2014

June

127

2

North

2014

May

645

1

South

2013

May

367

3

 

How can I do this please ? 

Thank you for your help.

Betty

 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

If you want to maintain the original order in the data, you can use a hash object like this

 

data have;
input Region $ Year Month $ Sales;
datalines;
North 2014 May  245 
North 2014 May  347 
North 2014 June 127 
North 2014 May  645 
South 2013 May  367 
;

data want(drop = c);
   if _N_ = 1 then do;
      dcl hash h();
      h.definekey('Region', 'Year', 'Month');
      h.definedata('RYMkey');
      h.definedone();
   end;
 
   set have;

   if h.find() ne 0 then do;
      c + 1;
      RYMkey = c;
   end;

   h.replace();

run;

 

Result:

 

Region  Year  Month  Sales  RYMkey 
North   2014  May    245    1 
North   2014  May    347    1 
North   2014  June   127    2 
North   2014  May    645    1 
South   2013  May    367    3 

View solution in original post

5 REPLIES 5
PeterClemmensen
Tourmaline | Level 20

If you want to maintain the original order in the data, you can use a hash object like this

 

data have;
input Region $ Year Month $ Sales;
datalines;
North 2014 May  245 
North 2014 May  347 
North 2014 June 127 
North 2014 May  645 
South 2013 May  367 
;

data want(drop = c);
   if _N_ = 1 then do;
      dcl hash h();
      h.definekey('Region', 'Year', 'Month');
      h.definedata('RYMkey');
      h.definedone();
   end;
 
   set have;

   if h.find() ne 0 then do;
      c + 1;
      RYMkey = c;
   end;

   h.replace();

run;

 

Result:

 

Region  Year  Month  Sales  RYMkey 
North   2014  May    245    1 
North   2014  May    347    1 
North   2014  June   127    2 
North   2014  May    645    1 
South   2013  May    367    3 
Betty_sam
Obsidian | Level 7

Peter,

Perfect ! 

Thank you so much 

 

Betty

RichardDeVen
Barite | Level 11
Why did June change to May in 4th row ?
Betty_sam
Obsidian | Level 7

It's just a typographical error 

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1264 views
  • 1 like
  • 3 in conversation