Hi there,
I'm trying to read data from a table that has multiple 'dimensions'. At first, it is just a 3 way table like this:
smoker nonsmoker
male white 10 20
others 20 30
female white 30 40
others 30 30
and I just write:
input gender race smoker count;
cards;
1 1 1 10
1 1 2 20
...
however when the table becomes bigger and bigger and having more variables the task becomes quite hard (like the table in the attachment), and I'm wondering if there are any easier way to input data like that?
You could read it as and transpose it. data have; input var1 $ var2 $ smoker nonsmoker; cards; male white 10 20 . others 20 30 female white 30 40 . others 30 30 ; run; proc transpose data=have out=want; by var1 var2 notsorted; var smoker nonsmoker; run;
You could read it as and transpose it. data have; input var1 $ var2 $ smoker nonsmoker; cards; male white 10 20 . others 20 30 female white 30 40 . others 30 30 ; run; proc transpose data=have out=want; by var1 var2 notsorted; var smoker nonsmoker; run;
Thanks so much ksharp!
Is there any way I can do to rename the variable '_name_' in the transposed dataset which has the value of 'smoker' to a numerical variable 1, nonsmoker to 2?
also, how can I renane the variable col1 in the transposed variable?
options validvarname=any; data have; input var1 $ var2 $ '1'n '2'n; cards; male white 10 20 . others 20 30 female white 30 40 . others 30 30 ; run; proc transpose data=have out=want(rename=(col1=value)); by var1 var2 notsorted; var '1'n '2'n ; run;
options validvarname=any; data have; input var1 $ var2 $ '1'n '2'n; cards; male white 10 20 . others 20 30 female white 30 40 . others 30 30 ; run; proc transpose data=have name=smoke out=want(rename=(col1=value)); by var1 var2 notsorted; var '1'n '2'n ; run;
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.
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.