## Dynamic Varible in the table

Frequent Contributor
Posts: 130

# Dynamic Varible in the table

I have table A:

 i variable 1 x1 1 x2 1 x3 1 x4 1 x5 1 xi

How to have table B as :

 i variable 1 x1,x2,x3,x4,x5..xi

the problem is , 'xi' , 'i'  isdynamic value, from Proc Transpose Prefix option.

I need to know  HOW  from table A to B, and handle xi dynamically.

Thank you

Posts: 3,066

## Re: Dynamic Varible in the table

[ Edited ]

PROC TRANSPOSE is dynamic, there is no need for you to program the dynamic aspect of this operation.

--
Paige Miller
Frequent Contributor
Posts: 130

## Re: Dynamic Varible in the table

PROC Transpose the dynamic aspect of this operation result is multiple rows,

I need concatenate x1,x2,x3,x4..xi   to one record - one row  separated by ','.

Posts: 3,066

## Re: Dynamic Varible in the table

@JHE wrote:

I need concatenate x1,x2,x3,x4..xi   to one record - one row  separated by ','.

It's hard to imagine any useful reason to create this variable as a concatenation of the variable names.

--
Paige Miller
Super User
Posts: 23,776

## Re: Dynamic Varible in the table

I don’t understand the issue you’re facing. Can you explain in more detail?
Frequent Contributor
Posts: 130

## Re: Dynamic Varible in the table

When I have table A as PROC Transpose result.

Next step need to have table B .

How to have Table B from Table A

After Proc Transpose I have Table A, how many rows are unknown . ...'xi'. But customer need to concatenate xi rows to one recode--- one row.

Super User
Posts: 23,776

## Re: Dynamic Varible in the table

@JHE wrote:

When I have table A as PROC Transpose result.

Next step need to have table B .

How to have Table B from Table A

After Proc Transpose I have Table A, how many rows are unknown . ...'xi'. But customer need to concatenate xi rows to one recode--- one row.

You don't need to know how many. You can use a variable short cut with the PREFIX approach to concatenate all together.

``catx(", ", of my_prefix: );``

Using a naming convention and your dynamic approach will work. Or don't transpose and do it line by line and output at the last using LAST. Like in this question: https://communities.sas.com/t5/Base-SAS-Programming/vertical-concatenation-based-on-grouping-variabl...

Frequent Contributor
Posts: 130

## Re: Dynamic Varible in the table

oh, I see,      CATX(',', of my_prefix: ) , I have problem to get  'of my_prefix: ' ,

PROC TRANSPOSE DATA=FACETS_PROVIDERa PREFIX=TYPE OUT=FACETS_PROVIDERb (drop=_name_ _label_);

by lob service_provider_id;

var type_of_service;

RUN;

From this code, I could have Type1, type2......typen, 'n' would be my problem, do not know how to get it.

Affect my next step CATX(',',type1, type2...typen)

Super User
Posts: 23,776

## Re: Dynamic Varible in the table

Note the colon and try it and tell me if it doesn't work.
catx(', ', of TYPE;
Frequent Contributor
Posts: 130

## Re: Dynamic Varible in the table

 i x1 x2 x3 x4 x5 xi

PROC Transpose result, since xi 'i' is unknown, when I need to use

CATX(',',x1,x2,x3....xi), but could not decide 'i'..

Super User
Posts: 23,776

# 6 easy ways to specify a list of variables in SAS

https://blogs.sas.com/content/iml/2018/05/29/6-easy-ways-to-specify-a-list-of-variables-in-sas.html

``X_COMBO = catx(", ", of X:);``

Frequent Contributor
Posts: 130

## Re: Dynamic Varible in the table

PROC TRANSPOSE DATA=a PREFIX=TYPE OUT=b (drop=_name_ _label_);
by lob;
var typ;
RUN;

Result of running above , have Table b:

 LOB TYpe1 Type2 Type3 ..TYPE..i 100 MHSC MHO1 MHSA ….?

Note: Type..i would be unknown, dynamic.

Want to have a new table C: I am a SQL person ...

PROC SQL;
CREATE TABLE c as
SELECT LOB,
CATX(',',of TYPE AS Service_Codes,
FROM B;
QUIT;

I want result :

 LOB TYPE 100 MHSC,MHO1,MHSA,…….TYPEi….

would not working ....???

Super User
Posts: 23,776

## Re: Dynamic Varible in the table

SQL doesn't support variable lists so you'll need to know the i value or use a data step instead.

Super User
Posts: 13,583

## Re: Dynamic Varible in the table

@JHE wrote:

I have table A:

 i variable 1 x1 1 x2 1 x3 1 x4 1 x5 1 xi

How to have table B as :

 i variable 1 x1,x2,x3,x4,x5..xi

the problem is , 'xi' , 'i'  isdynamic value, from Proc Transpose Prefix option.

I need to know  HOW  from table A to B, and handle xi dynamically.

Thank you

If I understand what you want you will likely have an issue with assigning a length to the new variable that contains all of those values.

If the x1 , x2 etc are numeric you also need to tell us what precision you need for each value as the only way to accomplish anything close the that would require turning all of the values into character values so that you could combine them into a single string.

Perhaps you can share what you will actually do with that resulting data set as perhaps this step isn't needed at all.

Frequent Contributor
Posts: 130

## Re: Dynamic Varible in the table

using proc sql, because we need group by LOB,

result should be as this ...

 LOB TYPE 100 MHSC,MHO1,MHSA,…….TYPEi…. 200 MHSC,MHO1,MHSA,…….TYPEi…. 300 MNO1
Discussion stats
• 22 replies
• 132 views
• 5 likes
• 5 in conversation