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

Hello
I use the following macro to transpose my tables according to a single variable and I would like to introduce a second variable.

Can anyone help me for this?

Thanks

%macro transpose_B(	
						vis=,
						vis2=
					 );

			data table_&vis;
			set table;
			if visit=&vis2;
			keep id visit var1 var2 var3 var4 var5 var6 ;
			
		run;
		
		proc sort data=table_&vis nodupkey;
			by id ;
		run;

		
		data table_&vis;
			set table_&vis;
			drop VISIT;
			rename var1=var1_&vis; rename var2=var2_&vis; rename var3=var3_&vis; rename var4=var4_&vis; rename var5=var5_&vis;  rename var6=var6_&vis;
			
		run;

	
	proc sort data=table_&vis;
		by id;
	run;

%mend;

/*Run the transpose_B macro */

%transpose_B(vis=D1, vis2="Baseline");
1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

@Liamb The %TRANSPOSE macro that I gave a link to will do this.

--
Paige Miller

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

It's not clear to me what you mean by "introduce a second variable". What would this second variable do?

 

It's also not clear to me how this is a transpose.

 

Nevertheless, TRANSPOSE macros are available, you don't have to write your own.

https://www.sas.com/content/dam/SAS/en_ca/User%20Group%20Presentations/TASS/Tabachneck-FlipTranspose...

https://communities.sas.com/t5/SAS-Communities-Library/A-better-way-to-FLIP-i-e-transpose-make-wide-...

 

--
Paige Miller
Liamb
Obsidian | Level 7
I would like to transpose on 2 variables, for example visit and sex
PaigeMiller
Diamond | Level 26

Use the %TRANSPOSE macro at the link I gave

--
Paige Miller
ballardw
Super User

Repeated use of code like this with the same data set on Set and Data statements in the body of macro code can make it extremely difficult to trace exactly where your logic had an error when debugging the data.

data table_&vis;
   set table_&vis; 

 

I also agree with @PaigeMiller that there is no "transposing" going on here. All I see is sub-setting a dataset and renaming (clumsily) a bunch of variables.

With that in mind, what do you intend to do with that second variable?

 

Show the code that performed the entire task you are attempting, using two variables, without any macro code. It is extremely difficult to program macros properly without non-macro code that works before attempting the macro. The SAS macro language creates code statements. If you do not know what statements you need to generate then you won't be able to code a macro to create them.

Liamb
Obsidian | Level 7

I have a dataset as below.

IDVISITDATELAB_IDLAB_VLAB_ULAB_NORMAL
A1D115/04/2022HB12d/dlYES
A1D216/04/2022CREAT20umol/lYES
A1D317/04/2022CRP100mg/lNO
A1D418/04/2022AST70U/lYES
A1D519/04/2022ALT90U/lNO
A2D120/04/2022HB10 YES
A2D221/04/2022CREAT30 YES
A2D322/04/2022CRP80 YES
A2D423/04/2022AST50 YES
A2D524/04/2022ALT40 YES
A3D125/04/2022HB6 NO
A3D226/04/2022CREAT10 NO
A3D327/04/2022CRP400 NO
A3D428/04/2022AST200 NO
A3D529/04/2022ALT200 NO

The macro I sent you allows me to transpose the table and add the name of the visit to the name of each variable then I do a merge of all my tables (table_d1 table_d2 table_d3 table_d4 table_d5).
And I want to transpose my table with visit and lab_id which will give me table_hb_d1 table_hb_d2.......

PaigeMiller
Diamond | Level 26

@Liamb The %TRANSPOSE macro that I gave a link to will do this.

--
Paige Miller

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