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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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