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

Hi All,

kindly find below sample input code.

Data t;
input NAME $5. 
ID  $3.
STATUS  $5.	
Reason_1 $4.
Reason_2 $4.	
Reason_3 $4.	
Reason_4 $4.	
Reason_5 $4.
Reason_6 $4.
Reason_7 $4.
Reason_8 $4.
Reason_9 $4.	
Reason_10 $4.	
Reason_11 $4.	
Reason_12 $4.	;
datalines;
44104 DD NA  AAG AAG AAG AAG AAG AAG AAG AAG AAG AAG AAG AAG AAG
8855     NA  AAG AAG AAG AAG AAG AAG AAG AAG AAG AAG AAG AAG AAG
26910    AC  TXN TXN TXN TXN TXN TXN TXN TXN TXN TXN TXN TXN TXN
8855     NA  TXN TXN TXN TXN TXN TXN TXN TXN TXN TXN TXN TXN TXN
44104 DD NA  TXN TXN TXN TXN TXN TXN TXN TXN TXN TXN TXN TXN TXN
44104 DD NA  BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN
8855     NA  CAH CAH CAH CAH CAH CAH CAH CAH CAH CAH CAH CAH CAH
8855     NA  FAL FAL FAL FAL FAL FAL FAL FAL FAL FAL FAL FAL FAL
26910    AC  RTM RTM RTM RTM RTM RTM RTM RTM RTM RTM RTM RTM RTM
8855     NA  UPD UPD UPD UPD UPD UPD UPD UPD UPD UPD UPD UPD UPD
44104 DD NA  SUC SUC SUC SUC SUC SUC SUC SUC SUC SUC SUC SUC SUC
26910    AC  SUC SUC SUC SUC SUC SUC SUC SUC SUC SUC SUC SUC SUC
;
run;



kindly find the below desired output that i need using above data please.

 

NAMEIDStatusReason_1Reason_2Reason_3Reason_4Reason_5Reason_6Reason_7Reason_8Reason_9Reason_10Reason_11Reason_12
44104DDNAAAG     TXNBANSUC        
8855 NAAAG     TXNCAHFAL UPD      
26910 ACTXNRTMSUC         
1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

@JJP1 

The following fully working code sample will hopefully give you all the guidance you need to set this up using DIS. 

DIS will generate a bit different but logically equivalent code to the sample below.

/*** Transpose Transformation **/

/* source table as input to the transpose */
data source;
  do group=1 to 5;
    do myvar=1 to 4;
      output;
    end;
  end;
  stop;
run;

/* Use Transpose transformation 
   - table "transposed" is output as "green" table object
*/
proc transpose data=source out=transposed;
  by group;
  var myvar;
run;


/** SQL Delete Transformation **/

/* automatically generated by SQL Delete Transformation */
%let _output=work.target;

/* pre-code added to SQL delete transformation 
   - (if target table SAS table. Not required if target table in a database)
*/
proc datasets lib=%scan(work.&_output,-2,.) nolist nowarn;
  delete %scan(&_output,-1,.);
  run;
quit;


/* 
   - create metadata for target table in the required stable structure (will all the potential columns in it).
   - connect this metadata table as output to the SQL Delete transformation
      - The SQL Delete transformatio will create an empty table structure if the table doesn't exist 
        (and we've just dropped the table in the previous step)
      - For a database: use the Truncate option as this speeds up the delete operation
          - and do not use the pre-code as always re-creating a table in a database is not a good practice
            .....but we need to do it if it's a SAS table as there records get only logically deleted
*/
data target;
  attrib group col1-col12 length=8;
  stop;
run;

/** User Written Transformation **/
/* connect green output table from transformation as input,
   connect target table as output (same as used above already)
*/

/* generated by transformation */
%let _input=work.transposed;
%let _output=work.target;

/* user written code */
proc append base=&_output data=&_input force nowarn;
run;

View solution in original post

24 REPLIES 24
PeterClemmensen
Tourmaline | Level 20

Please explain the logic on how to get from your data to your desired output?

JJP1
Pyrite | Level 9

Thanks @PeterClemmensen .kindly find the  attached sample  input data and the output i need please

andreas_lds
Jade | Level 19

@JJP1 wrote:

Thanks @PeterClemmensen .kindly find the  attached sample  input data and the output i need please


Please post data in usable form: a data step using datalines-statement and describe what should be done.

JJP1
Pyrite | Level 9

iam sorry kindly help whether you are able to see the sample data pasetd please.it is taking more space while pasting.so i have provided screen shot please

sampleinput.PNGdesiredoutput.PNG

Ksharp
Super User
data have;
input x $;
cards;
Staus	ID	NAME	Reason_0	Reason_1	Reason_2	Reason_3	Reason_4	Reason_5	Reason_6	Reason_7	Reason_8	Reason_9	Reason_10	Reason_11	Reason_12
NA	DD	44104	AAG                  	AAG                                     	AAG                                     	AAG                                     	AAG                                     	AAG                                     	AAG                                     	AAG                                     	AAG                                     	AAG                                     	AAG                                     	AAG                                     	AAG                                     
NA	                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                	8855	AAG                             	AAG                                     	AAG                                     	AAG                                     	AAG                                     	AAG                                     	AAG                                     	AAG                                     	AAG                                     	AAG                                     	AAG                                     	AAG                                     	AAG                                     
Accepted                                                                                                                                                                                                                                                       	                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                	26910	Txn                                       	Txn                                       	Txn                                       	Txn                                       	Txn                                       	Txn                                       	Txn                                       	Txn                                       	Txn                                       	Txn                                       	Txn                                       	Txn                                       	Txn                                       
NA	                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                	8855	Txn                                       	Txn                                       	Txn                                       	Txn                                       	Txn                                       	Txn                                       	Txn                                       	Txn                                       	Txn                                       	Txn                                       	Txn                                       	Txn                                       	Txn                                       
NA	DD	44104	Txn                                       	Txn                                       	Txn                                       	Txn                                       	Txn                                       	Txn                                       	Txn                                       	Txn                                       	Txn                                       	Txn                                       	Txn                                       	Txn                                       	Txn                                       
NA	DD	44104	Bank	Bank	Bank	Bank	Bank	Bank	Bank	Bank	Bank	Bank	Bank	Bank	Bank
NA	                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                	8855	Cache                                             	Cache                                             	Cache                                             	Cache                                             	Cache                                             	Cache                                             	Cache                                             	Cache                                             	Cache                                             	Cache                                             	Cache                                             	Cache                                             	Cache                                             
NA	                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                	8855	Failed                                            	Failed                                            	Failed                                            	Failed                                            	Failed                                            	Failed                                            	Failed                                            	Failed                                            	Failed                                            	Failed                                            	Failed                                            	Failed                                            	Failed                                            
Accepted                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                	                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                	26910	Real-time                                         	Real-tim                                          	Real-tim                                          	Real-tim                                          	Real-tim                                          	Real-tim                                          	Real-tim                                          	Real-tim                                          	Real-tim                                          	Real-tim                                          	Real-tim                                          	Real-tim                                          	Real-tim                                          
NA	                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                	8855	Update                                   	Update                                   	Update                                   	Update                                   	Update                                   	Update                                   	Update                                   	Update                                   	Update                                   	Update                                   	Update                                   	Update                                   	Update                                   
NA	DD	44104	Success                                           	Success                                           	Success                                           	Success                                           	Success                                           	Success                                           	Success                                           	Success                                           	Success                                           	Success                                           	Success                                           	Success                                           	Success                                           
Accepted  
;
data want;
 set have;
 retain found;
 if not found then output;
 if x='Accepted' then found=1;
 drop found;
run;
JJP1
Pyrite | Level 9

Thanks @Ksharp ,but iam not getting the expected output as desired,the same values are getting filled  on other reason columns please.

@andreas_lds ,please find the sample input below :

 

Staus

ID

NAME

Reason_0

Reason_1

Reason_2

Reason_3

Reason_4

Reason_5

Reason_6

Reason_7

Reason_8

Reason_9

Reason_10

Reason_11

Reason_12

NA

DD

44104

AAG                 

AAG                                    

AAG                                    

AAG                                    

AAG                                    

AAG                                    

AAG                                    

AAG                                    

AAG                                    

AAG                                     

AAG                                    

AAG                                    

AAG                                    

NA

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                

8855

AAG                            

AAG                                    

AAG                                    

AAG                                    

AAG                                    

AAG                                     

AAG                                    

AAG                                    

AAG                                    

AAG                                    

AAG                                    

AAG                                    

AAG                                     

Accepted                                                                                                                                                                                                                                                       

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                

26910

Txn                                      

Txn                                      

Txn                                       

Txn                                      

Txn                                      

Txn                                      

Txn                                      

Txn                                      

Txn                                       

Txn                                      

Txn                                      

Txn                                      

Txn                                      

NA

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                

8855

Txn                                      

Txn                                      

Txn                                      

Txn                                      

Txn                                       

Txn                                      

Txn                                      

Txn                                      

Txn                                      

Txn                                      

Txn                                       

Txn                                      

Txn                                      

NA

DD

44104

Txn                                      

Txn                                      

Txn                                       

Txn                                      

Txn                                      

Txn                                      

Txn                                      

Txn                                      

Txn                                       

Txn                                      

Txn                                      

Txn                                      

Txn                                      

NA

DD

44104

Bank

Bank

Bank

Bank

Bank

Bank

Bank

Bank

Bank

Bank

Bank

Bank

Bank

NA

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                

8855

Cache                                            

Cache                                            

Cache                                             

Cache                                            

Cache                                            

Cache                                            

Cache                                            

Cache                                             

Cache                                            

Cache                                            

Cache                                            

Cache                                            

Cache                                             

NA

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                

8855

Failed                                           

Failed                                           

Failed                                            

Failed                                           

Failed                                           

Failed                                           

Failed                                           

Failed                                            

Failed                                           

Failed                                           

Failed                                           

Failed                                           

Failed                                            

Accepted                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                

26910

Real-time                                        

Real-tim                                         

Real-tim                                          

Real-tim                                         

Real-tim                                         

Real-tim                                         

Real-tim                                         

Real-tim                                          

Real-tim                                         

Real-tim                                         

Real-tim                                         

Real-tim                                         

Real-tim                                          

NA

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                

8855

Update                                  

Update                                  

Update                                   

Update                                  

Update                                  

Update                                  

Update                                  

Update                                  

Update                                   

Update                                  

Update                                  

Update                                  

Update                                  

NA

DD

44104

Success                                          

Success                                          

Success                                          

Success                                          

Success                                          

Success                                          

Success                                          

Success                                          

Success                                          

Success                                          

Success                                          

Success                                          

Success                                          

Accepted                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                

26910

Success                                          

Success                                          

Success                                          

Success                                          

Success                                          

Success                                          

Success                                          

Success                                          

Success                                          

Success                                          

Success                                          

Success                                          

Success                                          

 

 

Expected output as below please :

 

 

 

IDNAMEStatusReason_0Reason_1Reason_2Reason_3Reason_4Reason_5Reason_6Reason_7Reason_8Reason_9Reason_10Reason_11Reason_12
DD44104NAAAG     Txn      BankSuccess         
 8855NAAAG     Txn      CacheFailed Update       
 26910AccpetedTxnReal-time        Success                 

 

 

 

 

Sorry to trouble all please help

andreas_lds
Jade | Level 19

Why do have 12 variables with the same content. Is this always the case? If not please post data matching what you have. And please explain the logic you want applied.

JJP1
Pyrite | Level 9

Hi @andreas_lds ,

 

Why do have 12 variables with the same content. Is this always the case? If not please post data matching what you have. And please explain the logic you want applied.

 

the values for 12 variables are taken from the source(oracle) table where these 12 reasons are
storing it as under single column called "Reason".(please note that this table can have maximum 12 reasons
or it might have less than this please for each "NAME" column).kindly have a look as sample input source code data below

 

 Data t;
input NAME $5. 
ID  $3.
STATUS  $5.	
Reason $4.;
datalines;
44104 DD NA  AAG 
8855     NA  AAG 
26910    AC  TXN 
8855     NA  TXN 
44104 DD NA  TXN 
44104 DD NA  BAN 
8855     NA  CAH 
8855     NA  FAL 
26910    AC  RTM 
8855     NA  UPD 
44104 DD NA  SUC 
26910    AC  SUC 
;
run;

and what in SAS i was told to do is to create the SAS dataset where to create 12 reason columns
and display ID,NAME and status and corresponding reasons for each record
and iam strugging to get as below ouput.i tried with transpose but was not properly output was not coming as desired with 12 values.

please please help

 

NAMEIDStatusReason_1Reason_2Reason_3Reason_4Reason_5Reason_6Reason_7Reason_8Reason_9Reason_10Reason_11Reason_12
44104DDNAAAG     TXNBANSUC        
8855 NAAAG     TXNCAHFAL UPD      
26910 ACTXNRTMSUC         
andreas_lds
Jade | Level 19

Why should Reason_5 be skipped for Name=8855?

 

Using proc transpose is afaik the shortest way to get what you want.

JJP1
Pyrite | Level 9

thanks @andreas_lds .i do not want to skip any NAME or any reason please.

i tried proc transpose option.it will create just with 6 columns please.

Actually i do n't want to manually create the columns 12 for reason value,please help .i have pasted sample inout data and output in previous chat please 

andreas_lds
Jade | Level 19

@JJP1 wrote:

thanks @andreas_lds .i do not want to skip any NAME or any reason please.

i tried proc transpose option.it will create just with 6 columns please.

Actually i do n't want to manually create the columns 12 for reason value,please help .i have pasted sample inout data and output in previous chat please 


I don't understand the first sentence. Where is anything skipped? I can't see any rule or reason explaining why Reason_5 is missing, but Reason_6 has a value for Name=8855. And as long as you don't  provide rules, it is simply impossible to write code that matches your expected result.

 

To solve the issue using proc transpose is the easiest way, because adding the missing variable is simple: just create a zero-obs dataset with all variables, then use another data step to append the result of proc transpose to the zero-obs dataset.

 

You could, of course use by in a data step and create the requested result in one step (sorting before is still necessary), but that way you will have to write more code.

JJP1
Pyrite | Level 9

Thanks @andreas_lds ,

kindly find below SAS DI job snapshot.where iam using transpose transformation which is currently creating the reasons based on source data.in transpose out reason 5 columns are now getting created in Test environmnet  please.

So in final target  table also i have manually created 12 columns for reason which are currently no data as expected.

but what my challenge is : in future ,transpose output got created with 7 or 8 reason columns.then currently there is no column mapping (for reaosn 6,reason 7 .....reason12)from transpose mappiing to table loader mapping transofmration.

These job will be running automatically as part of batch.

please find the attached mapping where mapping missing for reason7 to reason12 please as there is no input from tranpose output please.

but this is not the same case,it may have many reason with maximum 12 values please.

please help.iam sorry if iam not conveyed my problem correctly please.kindly help.i did not cleary understood how append option will help on this please.kindly help please

 

 

andreas_lds
Jade | Level 19

@JJP1  you should have mentioned that you are using SAS DI in your initial post. All suggestion made so far assumed, that you are using are sas programming environment. i don't have access to SAS DI and don't know how to fix the problem with the tools available there.

 

In the "normal" SAS world you can do something like this (code untested due to having no time to work on this):

data work.dummy;
  if 0 then set <name of the transposed dataset>;
  length Reason_1-Reason_12 $ 3;
  call missing(of Reason_:);
  stop;
run;

data work.final;
  set work.dummy <name of transposed dataset>;
run;
Patrick
Opal | Level 21

@JJP1 

ETL processes require stable table structures - something a Transpose doesn't provide. I've personally always considered the implementation of an out-of-the-box DIS Transpose transformation as a questionable design decision. 

Changing table structures are also something difficult to work with later on (i.e. for reports) and though should imho be avoided as far as possible for anything else than adhoc jobs.

 

Looking at the picture you've posted it appears that you just want to transpose the data into a final target table which always gets fully replaced. So in this scenario couldn't you just have this final table as output of the transpose transformation itself (= a permanent yellow table) and get rid of the table loader? As far as I remember the transpose transformation also gives you the option to update target metadata. As long as you don't try to use this target table as source for some other ETL process nothing should break anymore.

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 24 replies
  • 2832 views
  • 3 likes
  • 6 in conversation