BookmarkSubscribeRSS Feed
Newbie_23
Obsidian | Level 7

data have;
infile cards dlm=',';
informat product_Id $8. type $8. Description $20.;
input Product_ID Type Description;
cards;
12, A, Made of wood
12, B, Made of wood
25, C, Made of steel
15, D, Made of iron
12, F, Made of paper
;

proc sort data=have;
by product_id description type;
run;

data want;
set have;
length type_cat $20.;
by product_id description type;
retain type_cat;
if first.product_id then call missing(type_cat);
type_cat = catx(", ", type_cat, type);

if last.description then do;
description = catt(description, " (", type_cat, ")");
output;
end;

run;

I'm expecting output to be like shown below, please help on what is missing. Please note that the type is added to the description within paranthesis ,only when description is the same between the products.
I'm expecting output to be like shown below
Product_ID  description
12                  Made of paper(F)
12                 Made of Wood(A,B)
25                 Made of steel (C)
15                Made of iron ( D)

2 REPLIES 2
ballardw
Super User

Retain keep values until replaced. For your desired output you want to RESET the TYPE_CAT variable when the Description changes not Product_id as you have multiple descriptions for the value of Product_id.

if first.description then call missing(type_cat);

Caution: when you append or insert values into an existing variable you have a high probability of exceeding the length of the existing variable. If your data actually has 3 values of Type associated with one of the descriptions I expect to see truncated data. The space before (, plus (), 3 letters, 2 commas and 2 spaces following the comma adds 10 characters so if the base length of the description is 11 or more then the appended value will get truncated using the existing length of Description.

 

You might consider whether you actually need to append the data to the existing variable. For many purposes you may find using the variable Type_cat is more flexible. Of if that is even needed.

 

Good job on provided a working data step.

Better is to paste the code in either a text or code box opened using the </> or "running man" icons above the message window to preserver formatting. The boxes also help set code apart from the discussion.

PaigeMiller
Diamond | Level 26

@Newbie_23 

 

"SAS Code, please help" is not an acceptable title/subject line. Please go back to your original post (if possible) and edit the subject line so that it briefly describes the problem, as you have done in other threads.

--
Paige Miller

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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