BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Sathya3
Obsidian | Level 7
I have two columns id,type
Each Id can have more than one type. Whenever Id has more than one type, type has to be concatenated with ID. When ID has only one type ,then type should not be concatenated with ID
Sample input data
ID Type
1 A
1 B
2 A
3 D
Output expected :
ID Type
1A A
1B B
2 A
3 D


1 ACCEPTED SOLUTION

Accepted Solutions
Quentin
Super User

Thanks for sharing your code, and sample data.

 

You code is very close to working.  I only see two problems.

 

In the SQL step, you are using count(*) grouped by ID and Type.  This will tell you that there are two records for jan class1 and one record for jan class2.  But it doesn't tell you that there are two different values of TYPE for jan.  For that, you can use count(distinct(type)) grouped by ID.

 

In the data step, you need to increase the length of ID, or the value will be truncated.  

 

I think this code will give what you want:

 

data one;
input ID$ Type$ origin$;
datalines;
jan class1 UK
jan class1 UAE
jan class2 US
feb class3 INDIA
feb class3 MIDEAST
mar class4 NORWAY
;
run;

Proc sql;
  create table temp as
  Select *,count (distinct(Type)) as N from one 
  group by ID;
quit;

data two;
  length ID $12 ;
  set temp;
  if N>1 then ID=cats(ID,Type);
run;

proc print data=two ;
run ;
The Boston Area SAS Users Group (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.

View solution in original post

16 REPLIES 16
PaigeMiller
Diamond | Level 26

Seems like a homework assignment. Show us what you have tried.

--
Paige Miller
Sathya3
Obsidian | Level 7
It is part of a huge requirement.I have simplified it for easier understanding.If you can provide the code for that ,it would be great.
Reeza
Super User

You can use this method and then merge the results back with the original data for the duplicates. 

The other option is a DoW loop but it will still do two passes of the data.

 

https://gist.github.com/statgeek/d583cfa992bf56da51d435165b07e96a

 

 

Quentin
Super User

It's hard to help you, without knowing what you're learning.  For example, have you learned about the BY statement?  

 

Seeing how you approach the problem will help people help you.  Are you thinking to do this with a DATA step? Or use PROC SQL? Or PROC FREQ?  Even if your code is not working, sharing it will help.

 

Also you should post working SAS code to create the example input data.  Typically that is done with a DATA step with a CARDS statement.  That saves people time in having to write that code themselves.

The Boston Area SAS Users Group (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.
Sathya3
Obsidian | Level 7

Hi, I have tried this..But it is not giving me desired output.Might be some complex programming is required for my requirement to be fulfilled.

Requirement is "If I have multiple Types for an ID ,then Type must be concatenated to ID field.

But, if we have only one type for an ID , then type need not be concatenated with ID field.Here,Jan has multiple types (Class1 and class2) .Hence, type must be concatenated with ID like Janclass1 ,Janclass2. But for feb and mar , concatenation need not happen since they have only one value for type ( class3 type for feb and  class4 type for mar).

 

The expected output should have following ID values : janclass1 , janclass1,janclass2,feb,feb,mar

Please do not use Origin field in the logic to acheive this.

 

data one;
input ID$ Type$ origin$;
datalines;
jan class1 UK
jan class1 UAE
jan class2 US
feb class3 INDIA
feb class3 MIDEAST
mar class4 NORWAY
;
run;

 

Proc sql;
create table temp as
Select ID,Type,count(*) as N from one
group by ID,Type;
quit;

 

data two;
set temp;
if N=1 then ID=cats(ID,Type);
run;

Kurt_Bremser
Super User
select id, count(distinct type) as n
from have
group by id
having n > 1;

will give you a dataset with the id values for which you want concatenating. In the follow-up step, you need to create a new id variable with sufficient length to accommodate the concatenated strings.

PaigeMiller
Diamond | Level 26

Assumes the data is sorted by ID (which you didn't say, so is that a valid assumption?)

 

data want;
    length id $ 8;
    set have;
    by id;
    if not (first.id and last.id) then id=cats(id,type);
run;
--
Paige Miller
Sathya3
Obsidian | Level 7

Hi, I have tried this..But it is not giving me desired output.Might be some complex programming is required for my requirement to be fulfilled.

Requirement is "If I have multiple Types for an ID ,then Type must be concatenated to ID field.

But, if we have only one type for an ID , then type need not be concatenated with ID field.Here,Jan has multiple types (Class1 and class2) .Hence, type must be concatenated with ID like Janclass1 ,Janclass2. But for feb and mar , concatenation need not happen since they have only one value for type ( class3 type for feb and  class4 type for mar).

 

The expected output should have following ID values : janclass1 , janclass1,janclass2,feb,feb,mar

Please do not use Origin field in the logic to acheive this.

 

data one;
input ID$ Type$ origin$;
datalines;
jan class1 UK
jan class1 UAE
jan class2 US
feb class3 INDIA
feb class3 MIDEAST
mar class4 NORWAY
;
run;

 

Proc sql;
create table temp as
Select ID,Type,count(*) as N from one
group by ID,Type;
quit;

 

data two;
set temp;
if N=1 then ID=cats(ID,Type);
run;

Sathya3
Obsidian | Level 7

Hi, I have tried this. But it is not giving me desired output. Might be some complex programming is required for my requirement to be fulfilled. Requirement is "If I have multiple Types for an ID ,then Type must be concatenated to ID field. But, if we have only one type for an ID , then type need not be concatenated with ID field.Here,Jan has multiple types (Class1 and class2) .Hence, type must be concatenated with ID like Janclass1 ,Janclass2. But for feb and mar , concatenation need not happen since they have only one value for type ( class3 type for feb and  class4 type for mar).   The expected output should have following ID values : janclass1 , janclass1,janclass2,feb,feb,mar Please do not use Origin field in the logic to achieve this. 

data one;
input ID$ Type$ origin$;
datalines;
jan class1 UK
jan class1 UAE
jan class2 US
feb class3 INDIA
feb class3 MIDEAST
mar class4 NORWAY
;
run;

Proc sql;
create table temp as
Select ID,Type,count(*) as N from one 
group by ID,Type;
quit;

data two;
set temp;
if N=1 then ID=cats(ID,Type);
run;
ballardw
Super User

Please do not post identical questions.

And how is this different than your question in https://communities.sas.com/t5/SAS-Programming/Programming-help-required/m-p/873986#M345268

 

 

yabwon
Onyx | Level 15

Search Google for "double DoW-loop".

data one;
input ID$ Type$ origin$;
datalines;
jan class1 UK
jan class1 UAE
jan class2 US
feb class3 INDIA
feb class3 MIDEAST
mar class4 NORWAY
;
run;

data want;
  length newID $ 16;
  do _N_ = 1 by 1 until(last.id);
    set one;
    by id notsorted;

    if first.id then n_type=0;
    if Type NE lag(Type) then n_type+1;
  end;

  do _N_ = 1 to _N_;
    set one;
    if n_type > 1 then newID = cats(ID,Type);
                  else newID = ID;
    output;
  end;

  drop ID n_type;
  rename newID = ID;
run;
proc print;
run;

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



PaigeMiller
Diamond | Level 26

Hello @Sathya3 

I'd like to request you provide meaningful (but brief) descriptions of the actual problem you are trying to solve as the title/subject of your post. Subject lines like "how to solve this" and "programming help required" are not helpful and don't describe the actual problem and could apply to virtually every post here in the SAS forums. We're trying to help you, please go ahead from now on and help us by creating meaningful description of the problem in the subject line

--
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
  • 16 replies
  • 1827 views
  • 0 likes
  • 7 in conversation