BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
TimStettner
Fluorite | Level 6

Hey,

 

I'm actually struggling a bit with the behaviour of the "good old" PROC TRANSPOSE and the CAS Action TRANSPOSE.

 

I want to transpose the four Petal/Sepal Columns by the column species

SpeciesPetalWidthPetalLengthSepalWidthSepalLength
Setosa1020100200
Versicolor5678
Virginica1234

 

Using

proc transpose data=iris2 out=iris4;
by Species;
quit;

would lead to:

Species_NAME_COL1
SetosaSepalLength200
SetosaSepalWidth100
SetosaPetalWidth10
SetosaPetalLength20
Versicolor......
Virginica......

 

So far, so good.

 

When I now switch to the new world in CAS and using CASL actions, the TRANSPOSE action behaves in another way.

Here you need to name an "ID" column, and in the output you will get a transposed column for each unique value of this one (and not the expected "COL1" column).

proc cas;
	transpose.transpose / 
		table={caslib="casuser", name="IRIS2", groupBy="Species"},
		id="Species",
		name="Variable",
		casOut={name="IRIS4", replace=TRUE, caslib="casuser"}
		;
quit;
SpeciesVariableSetosaVersicolorVirginica
VersicolorSepalLength 8 
VersicolorSepalWidth 7 
......   
VirginicaSepalLength  4

 

Ist this something you can change with settings? I do not find anything except the description of exactly this behaviour.

And you cannot skip the ID column (as in PROC TRANSPOSE) as this is a required parameter.

 

To merge the three columns in an additional step afterwords is possible, but there should be a smarter solution, right?

Any ideas? 

 

Cheers, Tim

1 ACCEPTED SOLUTION

Accepted Solutions
DerylHollick
SAS Employee

You can create an id column with the names you want. 

 

proc cas;
	transpose.transpose / 
		table={caslib="casuser", name="IRIS2", groupBy="Species", 
               computedVars={name="colname"} computedVarsProgram="colname='COL1';"},
		id="colname",
		name="Variable",
		casOut={name="IRIS4", replace=TRUE, caslib="casuser"}
		;
quit;

View solution in original post

3 REPLIES 3
DerylHollick
SAS Employee

You can create an id column with the names you want. 

 

proc cas;
	transpose.transpose / 
		table={caslib="casuser", name="IRIS2", groupBy="Species", 
               computedVars={name="colname"} computedVarsProgram="colname='COL1';"},
		id="colname",
		name="Variable",
		casOut={name="IRIS4", replace=TRUE, caslib="casuser"}
		;
quit;
DavidHD
SAS Employee

Hey,

 

you can make use of the dataShaping.wideToLong CAS Action. Please note that this action was first introduced in SAS Viya 2021.1.2

 

proc cas;
	dataShaping.wideToLong /
		table={caslib='casuser', name='iris'},
		id='Species',
		casOut={caslib='casuser', name='iris4', replace=true}
	;
quit;

Best, David

TimStettner
Fluorite | Level 6

Great, thank you for your quick replies.

Both ways are working, but as we are actually still in Viya 3.5 I have to wait for the "wide to long" action until Viya 4.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1505 views
  • 5 likes
  • 3 in conversation