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

Hi,

 

I need to assign a customer category in a new variable (Derived_Customer_Cat) to each customer record based on the first valid value of a variable (Customer_Cat) for all records of the same customer (Customer_ID).  And the following code is a demonstration of what I have and want.

 

Can you help me find an efficient approach?  Thank you!

Jason

 

data have;
input Customer_ID $1 Customer_Cat $3;
datalines;
1 A
1 A
1 A
2
2 B
2 B
3
3
3 A
3 C
4
4
4 C
4 D
4 D
4
;
run;

 

data want;
input Customer_ID $1 Customer_Cat $3 Derived_Customer_Cat $5;
datalines;
1 A A
1 A A
1 A A
2 B
2 B B
2 B B
3 A
3 A
3 A A
3 C A
4 C
4 C
4 C C
4 D C
4 D C
4 C
;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

Please try 

 

data have;
input Customer_ID $1 Customer_Cat $3 ;
datalines;
1 A
1 A
1 A
2  
2 B
2 B
3  
3  
3 A
3 C
4  
4  
4 C
4 D
4 D
4 C
;
run;

data first;
set have;
by Customer_ID;
where Customer_Cat ne '';
if first.Customer_ID;
Derived_Customer_Cat =Customer_Cat;
keep Customer_ID Derived_Customer_Cat;
run;

data want2;
merge have(in=a) first(in=b) ;
by Customer_ID;
if a;
run;
Thanks,
Jag

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

... based on the first valid value ...

 

What is a "valid value"?

--
Paige Miller
JasonL
Quartz | Level 8

Sorry the datalines for "want" got shifted.  And here is what the data steps should be:

 

data have;
input Customer_ID $1 Customer_Cat $3;
datalines;
1 A
1 A
1 A
2
2 B
2 B
3
3
3 A
3 C
4
4
4 C
4 D
4 D
4
;
run;

data want;
input Customer_ID $1 Customer_Cat $3 Derived_Customer_Cat $5;
datalines;
1 A A
1 A A
1 A A
2    B
2 B B
2 B B
3    A
3    A
3 A A
3 C A
4    C
4    C
4 C C
4 D C
4 D C
4 C C
;
run;

Jagadishkatam
Amethyst | Level 16

Please try 

 

data have;
input Customer_ID $1 Customer_Cat $3 ;
datalines;
1 A
1 A
1 A
2  
2 B
2 B
3  
3  
3 A
3 C
4  
4  
4 C
4 D
4 D
4 C
;
run;

data first;
set have;
by Customer_ID;
where Customer_Cat ne '';
if first.Customer_ID;
Derived_Customer_Cat =Customer_Cat;
keep Customer_ID Derived_Customer_Cat;
run;

data want2;
merge have(in=a) first(in=b) ;
by Customer_ID;
if a;
run;
Thanks,
Jag

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