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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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