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;
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!
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.