BookmarkSubscribeRSS Feed
Sas_noob25
Obsidian | Level 7

I have a dataset of car sales by various locations, by month. I'm trying to see if a location sold the same model and color in consecutive months. Example below. 

 

Sas_noob25_0-1691086824569.png

I would want to only keep the locations that have sold the same make, model, color car in both January and February.

 

What's a good way to do this considering there are 100's of thousands of records to go through? 

 

Thanks in advance!

 

 

 

3 REPLIES 3
PaigeMiller
Diamond | Level 26

We can't see your screen capture, and even if we could see it, we cannot work from data in a screen capture.

 

Data should be provided as working SAS data step code (examples and isntructions). DO NOT PROVIDE DATA IN ANY OTHER FORMAT

--
Paige Miller
ballardw
Super User

Is your "month" variable actually a SAS date, that can reliably tell us January or February or just some random number?

Do you have data extending across more than one year? (goes to which January and February might be involved)?

Do you actually have "months" other than January and February (assuming ending in 01 means Jan and 02 means Feb)?

 

Actually concerned with Jan and Feb or consecutive months? Different approaches. The values of month are critical if you expect month to cross calendar year such as Dec to Jan and treat them as consecutive. 202112 is generally not followed by 202201 (not consecutive) which is why see need to know about your date variable in more detail.

 

mkeintz
PROC Star

if

  1.  Your data are sorted chronologically
  2. MONTH is stored as a sas date variable, always taking the 1st day of the indicated month,

then:

data want;
  set have;
  if _n_=1 then do;
    declare hash h (dataset:'have(obs=0)');
      h.definekey(all:'Y');
      h.definedone();
  end;
  h.add();
  if h.check(key:location,key:make,key:model,key:color,key(intnx('month',month,-1))=0 
     then sold_same_model_last_month='Yes'
     else sold_same_model_last_month='No-'
run;

Of course, in the absence of sample data in the format of a working DATA step, this code is untested.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

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
  • 3 replies
  • 338 views
  • 0 likes
  • 4 in conversation