BookmarkSubscribeRSS Feed
Stillmatic
Calcite | Level 5

I'm not well versed in SAS language, just enough to get by with basic logic in regards to data and proc sql steps, maybe a sort here and there.

Hoping for some direction on a problem I'm facing.

Dataset

DATECUST_IDSYSTEMCOLOR
3/31/202115ORABLUE
4/30/202113ORABLUE
5/31/202110ORABLUE
6/30/202119ORABLUE
7/31/202111ORABLUE
8/31/202114ORABLUE
9/30/202121ORABLUE
10/31/202116ORABLUE
11/30/202112ORABLUE
12/31/202117ORABLUE
1/31/202222ORABLUE
2/28/202218ORABLUE
3/31/202220SISUNK
4/30/202223SISUNK

 

The issue: When a customer switches to another system their color data goes missing. Hence the return value of unknown. What I need assistance in is coming up with the right logic to assign the unknown fields to the last known value. Now it doesn't have to be the last known value as theoretically the color should never change once set, until it loads into another system.

 

So the dataset I want to return is:

DATECUST_IDSYSTEMCOLOR
3/31/202115ORABLUE
4/30/202113ORABLUE
5/31/202110ORABLUE
6/30/202119ORABLUE
7/31/202111ORABLUE
8/31/202114ORABLUE
9/30/202121ORABLUE
10/31/202116ORABLUE
11/30/202112ORABLUE
12/31/202117ORABLUE
1/31/202222ORABLUE
2/28/202218ORABLUE
3/31/202220SISBLUE
4/30/202223SISBLUE

 

Thanks for your help in advance!

1 REPLY 1
mkeintz
PROC Star

In the case of unknown color you want to "assign the unknown fields to the last known value."   Apparently, you are ok with assigning a color to CUST_ID 20 with a known value from CUST_ID 18.  Is that correct?

 

Assuming the answer is yes, then the code below does what you need (untested in the absence of sample data in the form of a working data step).

 

data want (drop=_:);
  set have;
  length _lastknowncolor $4;
  retain _lastknowncolor ;
  if   color^= 'UNK' then _lastknowncolor=color;
  else color=_lastknowncolor;
run;

The key here is the RETAIN statement which tells SAS not to reset the retained variable to missing with each new iteration of the data step (i.e. each incoming obs in the above case).  

 

--------------------------
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 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!

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
  • 1 reply
  • 303 views
  • 0 likes
  • 2 in conversation