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

Hi everyone,

 

I am new to SAS and SQL. I have a dataset with ID, class and the related values. It's like,

 

ID       CLASS            VALUE

1             A                     5

1             B                     7

1             C                    10

2             A                     7

2             B                     5

2             C                    12

 

Can anyone help to use SQL to create 3 variables A, B , C to store different class values for each ID

The expected output table looks like

 

 

ID    A    B    C

1     5     7    10

2     7     5     12

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Like this?

 

data have;
   input ID       CLASS $            VALUE;
datalines;
1             A                     5
1             B                     7
1             C                    10
2             A                     7
2             B                     5
2             C                    12
;
run;

proc transpose data=have out=want  (drop=_name_)
     let;
   by ID;
   id Class;
   var value;
run;

The data set is to have a concrete example and is the perferred way to show your data.

 

Proc Transpose is designed to do basic tranpostions from long to wide or wide to long data structures.

 

Note that ID used on the BY statement is your identification variable.

ID as used in ID Class; is the instruction that says "use the value of the variable class to create the identification (name) of the values.

View solution in original post

2 REPLIES 2
novinosrin
Tourmaline | Level 20

why not accomplish your req with a super simple proc transpose-

 

data have;

input  ID       CLASS $          VALUE;

datalines;

1             A                     5

1             B                     7

1             C                    10

2             A                     7

2             B                     5

2             C                    12

;

 

proc transpose data=have out=want(drop=_name_);

by id;

id class;

var value;

run;

ballardw
Super User

Like this?

 

data have;
   input ID       CLASS $            VALUE;
datalines;
1             A                     5
1             B                     7
1             C                    10
2             A                     7
2             B                     5
2             C                    12
;
run;

proc transpose data=have out=want  (drop=_name_)
     let;
   by ID;
   id Class;
   var value;
run;

The data set is to have a concrete example and is the perferred way to show your data.

 

Proc Transpose is designed to do basic tranpostions from long to wide or wide to long data structures.

 

Note that ID used on the BY statement is your identification variable.

ID as used in ID Class; is the instruction that says "use the value of the variable class to create the identification (name) of the values.

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