BookmarkSubscribeRSS Feed
deleted_user
Not applicable
I have four variables:
Location (x,y,z), q1, q2, and q3 (where each line is a different individual).
q1,q2, and q3 are categorical variables (Disagree, Agree, Strongly Agree).
I would like to create a data set with the following variables:
Location, Question (numeric variable ranging from 1-10), Disagree, Agree, Strongly_Agree
where the data set would look like this:
Location Question Agree Disagree Strongly_Agree
x 1 1 0 0
x 1 0 1 0
x 2 0 0 1
x 2 1 0 0
x 3 1 0 0
x 3 0 1 0
where each line is a different individual (no individual identifier is needed).
If a person agreed with a certain question they get a 1 for Agree and a zero for Disagree and Strong_Agree.
Thank you.
7 REPLIES 7
abdullala
Calcite | Level 5
I assume you mean q1,q2,q3 are 3 questions, with categoric values disagree, agree or stronly.
1.create 3 new variable to represent your category for q1,q2,q3. sicne there are only 3 categories, you may use agree='100',disagree='010',strongly='001'.
2.use proc transpose to get a verticle layout of the original data, for the 3 new variables.
3.parse new values into 3 category vars, ie. agree, disagree, strongly, eg. by using substr.
deleted_user
Not applicable
This looks like it could work.
I'm having some trouble setting up the proc transpose statement, though.
I did this part:
agree='100',disagree='010',strongly='001'
How do I write the proc transpose so that q1, q2, and q3 are all in one column named question (where question=1,2,3)?
Thank you.
abdullala
Calcite | Level 5
use var statement. check proc transpose documents for correct sytax and requirements. you may need to sort by location if you need to use by in proc transpose. you will get a column _name_ after transposing, with values of 'q1','q2', etc. you may extract the numeric part from that by substr.
seems that you actually have 10 instead of 3 questions? you may want to use array then. format will help translating the categoric values.
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
PROC TRANSPOSE converts "vertical" to "horizontal", not the other way. To accomplish the objective of taking a file as you have shown, it will take a Data step approach. The SAS support http://support.sas.com/ website has SAS-hosted documentation and supplemental technical and conference reference material. You can use the SEARCH facility at the website or use a Google advanced search argument as shown below:

vertical transpose sas dataset site:sas.com

Scott Barry
SBBWorks, Inc.

234-31: The TRANSPOSE Procedure or How to Turn It Around
Janet Stuelpner, Left Hand Computing, Inc., New Canaan, CT
http://www2.sas.com/proceedings/sugi31/234-31.pdf
abdullala
Calcite | Level 5
proc transpose does convert vertical to horizontal and vise versa. it worked perfectly for this purpose.

data a;
input Location $ q1 $ q2 $ q3 $;
cards;
x Disagree Agree Stronly
y Agree Disagree Stronly
x Stronly Disagree Disagree
z Stronly Stronly Disagree
x Disagree Stronly Agree
z Stronly Stronly Disagree
x Agree Disagree Stronly
;
run;
data a;
set a;
n=_n_;
run;
proc transpose data=a out=b;
var q1 q2 q3;
by n location;
run;
data_null__
Jade | Level 19
The key to a horizontal to vertical transpose with PROC TRANSPOSE as you have coded is the necessary evil, unique KEY. In your example. n=_n_;

My data, usually has a unique key so the coding a necessary evil is usually unnecessary. You should consider a data step view in for this task.
abdullala
Calcite | Level 5
you are absolutely right. the original author said he had a subject ID variable which he didn't need, otherwise that can be used as the unique key. I created the variable n=_n_ to omit the sorting procedure. all I wanted to illustrate in this example is that proc transpose does convert horizontal to vertical. happy SAS coding!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 7 replies
  • 802 views
  • 0 likes
  • 4 in conversation