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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 5 replies
  • 763 views
  • 1 like
  • 3 in conversation