BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Emma2021
Quartz | Level 8
Can I change those below sas codes into SQL?

Proc sort data=dsn; by id;

Proc transpose data=dsn; out=dsn_1;
By id;
Id name;
Var var1;
Run;

Thank you.
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Transposes are hard to do in SQL. In SAS SQL you would have to hard code the new variable names for example. Some SQL DB do support transpose using PIVOT or other similar functionality but none as nicely as PROC TRANSPOSE.

So technically you can, but the exact code depends on the data and if the data changes the code has to change.

 


@Emma2021 wrote:
Can I change those below sas codes into SQL?

Proc sort data=dsn; by id;

Proc transpose data=dsn; out=dsn_1;
By id;
Id name;
Var var1;
Run;

Thank you.

 

View solution in original post

2 REPLIES 2
Reeza
Super User

Transposes are hard to do in SQL. In SAS SQL you would have to hard code the new variable names for example. Some SQL DB do support transpose using PIVOT or other similar functionality but none as nicely as PROC TRANSPOSE.

So technically you can, but the exact code depends on the data and if the data changes the code has to change.

 


@Emma2021 wrote:
Can I change those below sas codes into SQL?

Proc sort data=dsn; by id;

Proc transpose data=dsn; out=dsn_1;
By id;
Id name;
Var var1;
Run;

Thank you.

 

Daniel_Enache
Calcite | Level 5

Hello,

as in the previous answer, I think that there is no general solution.

However, there is a solution, if you know the categories of your transpose-id-variable (in you case "Name"). Since I don't know your data set, I will use the class dat set shipped with SAS:

data Test;
	set sashelp.class;
run;
proc sort data=Test; by Name Sex; run;
proc transpose data=Test out=Test_1;
	id Sex;
	by Name;
	var Height;
run;

The Variable Sex has 2 categories 'F' and 'M'. If those are not changing, you can write:

proc sql;
	create table Test_2 as
		select Name,
			max( case when Sex = 'M' then Height else . end ) as M,
			max( case when Sex = 'F' then Height else . end ) as F
		from Test
		group by Name
		order by Name;
quit;

However, if those categories are changing, you will need to program a SAS-Makro to retrieve the categories into a list and do a loop:

%macro transCustom(intab, outtab, idvar, byvar, varvar);
	%local i numid listid;

	/* Get categories and store in a list. */
	proc sql noprint;
		select distinct &idvar.
		into :listid separated by ' '
		from &intab.
		order by &idvar.;
	quit;

	/* Number of elements in list. */
	%let numid=%sysfunc(countw(&listid.));

	proc sql;
		create table &outtab. as
			select &byvar.
				%do i=1 %to &numid.;
					%let curcat=%scan(&listid., &i.); /* Get i-th element from list. */
					, max( case when &idvar. = "&curcat." then &varvar. else . end ) as &curcat.
				%end;
			from &intab.
			group by &byvar.
			order by &byvar.;
	quit;
%mend;
%transCustom(Test, Test_3, Sex, Name, Height);

I hope this helps,

 

Daniel.

 

 

 

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