BookmarkSubscribeRSS Feed
mreynaud
Obsidian | Level 7

Hello,

 

The table below is what I currently have. Lets call it "Test" for this example

2021-11-24_13-16-52.png

What I want is to transpose the phone_type column and have its values contain the phone numbers from the phone_number column.

 

Here is the ideal table I want, we'll call it "Final"

2021-11-24_13-17-08.png

 

I've been trying the following code:

proc transpose data=final out=test;
id phone_type;
var phone_number;
run;

And get this error:

ERROR: The ID value "Home" occurs twice in the input data set.

 

Any help I can get would be wonderful!

6 REPLIES 6
mreynaud
Obsidian | Level 7

Hello,

 

I am trying to create a table called "Final" that will look like this (for this example I am using made up data):

Client_IDClient_NameCellHomeText Message Opt-in
1Kate(111)111-1111(101)010-1010No
2Jack (202) 020-2020 
3Shannon(333)333-3333(303)030-3030Yes
4John(444)444-4444(404) 040-4040No
5Juliet(555)555-5555 Yes
6Sayid(666)666-6666 No
7Claire (707) 070-7070 
8Alex(888)888-8888(808) 080-8080Yes
9Charlie(999)999-9999 No

 

The table I am working from is called "Test". I've been trying to use Proc Transpose for the columns Phone_Type to create two new columns. The two new columns would be Home and Cell and its values would contain the numbers from the Phone_Number column.

Client_IDClient_NamePhone_TypePhone_NumberText Message Opt-in
1KateCell(111)111-1111No
1KateHome(101)010-1010 
2JackHome(202) 020-2020 
3ShannonCell(333)333-3333Yes
3ShannonHome(303)030-3030 
4JohnCell(444)444-4444No
4JohnHome(404) 040-4040 
5JulietCell(555)555-5555Yes
6SayidCell(666)666-6666No
7ClaireHome(707) 070-7070 
8AlexCell(888)888-8888Yes
8AlexHome(808) 080-8080 
9CharlieCell(999)999-9999No

 

Here is the code I've been attempting to use 

proc transpose data=final out=test;
id phone_type;
var phone_number;
run;

I keep on getting an error that says the ID value "Home" occurs twice in the input data set.

 

Any help I can get would be wonderful!

PaigeMiller
Diamond | Level 26

UNTESTED CODE

 

proc transpose data=test out=final;
    by client_id client_name;
    var phone_number;
    id phone_type;
run;

Since we can't program from screen captures, we request that in the future you provide data as SAS data steps (instructions) and then we can give you tested code. So, @mreynaud , please, no more data as screen captures (and also do not provide data as file attachments, as many people will not download them), data only as SAS data steps.

--
Paige Miller
MayurChaudhari
Calcite | Level 5
Please refer the solution below .

Proc transpose data=final out=a;
by phone_type;
var phone number;
run;

Proc transpose data=a out=b (drop-_name_) ;
var coll col2 col3 col4>>add all colums created;
id phone type;
run;

If any doubts plz reach out me on LinkedIn http://linkedin.com/in/mayur-chaudhari-77010821b
mkeintz
PROC Star

I don't see the need for proc transpose here, since a self_merge of the dataset (merging cell phone records against corresponding home phone records) should do well:

 

data want;
  merge have (where=(phone_type='Cell') rename=(phone_number=cell))
        have (where=(phone_type='Home') rename=(phone_number=home) drop=text_msg_opt);
  by clientid client_name;
  drop phone_type;
run;

This program is untested in the absence of sample data in the form of a working DATA step.

 

But it will correctly produce the "test_message_opt_in" column, unlike the PROC TRANSPOSE solutions.

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

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

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 6 replies
  • 1955 views
  • 1 like
  • 5 in conversation