BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
joseph626
Obsidian | Level 7

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?


1.jpg
1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
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;

View solution in original post

5 REPLIES 5
Ksharp
Super User
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;

joseph626
Obsidian | Level 7

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?

Ksharp
Super User

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;

Ksharp
Super User


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;

joseph626
Obsidian | Level 7
THANKS again!!!

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
  • 5 replies
  • 2329 views
  • 3 likes
  • 2 in conversation