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

Hello,

I want to transpose a data array.

When the "Script" column contains M2 then I want the "Want" table to report the date in the M2 column.

Another example for the id "789319", "Script" contains M6 I want to report the date in the column "M6"
When there is no data I want it to remain empty

Thanks a lot for your help.

William

 

 

 

      Want     
IDScriptDATEFlag  IDM2M3M4M6Flag
235638Dts_IM2_ras23/01/20210  23563823/01/202124/02/202126/03/202127/05/20210
235638Promo_IM3_be24/02/20210  45781425/01/202128/02/202129/03/202130/06/20211
457814SZE_IM2_rz25/01/20211 => 89764331/07/2021   0
235638Queffrti_IM4_rgg26/03/20210  89451201/09/202115/10/2021  1
235638AIU_IM6_ski27/05/20210  789319   15/12/20211
457814alop_IM3_rfz28/02/20211        
457814fop_IM4_rhtrh29/03/20211        
457814skop_IM6_rrefgr30/06/20211        
897643dzd_IM2_rsa31/07/20210        
894512uil_IM2_rdzd01/09/20211        
894512kujndbe_IM3_ezef15/10/20211        
789319kujnzze_IM6_ezff15/12/20211        
            
1 ACCEPTED SOLUTION

Accepted Solutions
Oligolas
Barite | Level 11

Hi,

you could do something like that:

DATA have;
infile datalines TRUNCOVER;
input ID :8. Script :$25. DATE :ddmmyy10. Flag :8.;
format date ddmmyy10.;
datalines4;
235638	Dts_IM2_ras	   23/01/2021 0
235638	Promo_IM3_be	24/02/2021	0
457814	SZE_IM2_rz	   25/01/2021	1
235638	Queffrti_IM4_rgg	26/03/2021	0
235638	AIU_IM6_ski	   27/05/2021	0
457814	alop_IM3_rfz	28/02/2021	1
457814	fop_IM4_rhtrh	29/03/2021	1
457814	skop_IM6_rrefgr 30/06/2021	1
897643	dzd_IM2_rsa	31/07/2021	0
894512	uil_IM2_rdzd	01/09/2021	1
894512	kujndbe_IM3_ezef	15/10/2021	1
789319	kujnzze_IM6_ezff	15/12/2021	1
;;;;
run;

data have;
   set have;
   length help $10;
   array ms M1-M10;
   do over ms;
      if index(script,vname(ms)) then help=vname(ms);
   end;
run;

proc sort data=have;by id flag;run;
proc transpose data=have out=want(drop=_name_);
   by id flag;
   var date;
   id help;
run;
________________________

- Cheers -

View solution in original post

3 REPLIES 3
andreas_lds
Jade | Level 19

I would start by  extracting the variable name from "script". Depending on the contents of "script" this could work:

varname = substr(scan(Script, 3, '_'), 2);

Next steps:

  • create a sorted list (in a macro-variable) of all m-variables to be created
  • sort data by id
  • maybe: use a data step, retain the m-variables, use first/last to fill the variables, this is tricky due to the non existence of m1 and m5 in your data, the appropriate position in the array could be returned by i = findw("&varlist", varname, ' ', 'set');

Post data in usable from, if you want working code.

Oligolas
Barite | Level 11

Hi,

you could do something like that:

DATA have;
infile datalines TRUNCOVER;
input ID :8. Script :$25. DATE :ddmmyy10. Flag :8.;
format date ddmmyy10.;
datalines4;
235638	Dts_IM2_ras	   23/01/2021 0
235638	Promo_IM3_be	24/02/2021	0
457814	SZE_IM2_rz	   25/01/2021	1
235638	Queffrti_IM4_rgg	26/03/2021	0
235638	AIU_IM6_ski	   27/05/2021	0
457814	alop_IM3_rfz	28/02/2021	1
457814	fop_IM4_rhtrh	29/03/2021	1
457814	skop_IM6_rrefgr 30/06/2021	1
897643	dzd_IM2_rsa	31/07/2021	0
894512	uil_IM2_rdzd	01/09/2021	1
894512	kujndbe_IM3_ezef	15/10/2021	1
789319	kujnzze_IM6_ezff	15/12/2021	1
;;;;
run;

data have;
   set have;
   length help $10;
   array ms M1-M10;
   do over ms;
      if index(script,vname(ms)) then help=vname(ms);
   end;
run;

proc sort data=have;by id flag;run;
proc transpose data=have out=want(drop=_name_);
   by id flag;
   var date;
   id help;
run;
________________________

- Cheers -

Kurt_Bremser
Super User

Preparing the data and using TRANSPOSE:

data long;
set have;
vname = substr(scan(script,2,"_"),2,2);
run;

proc sort data=long;
by id;
run;

proc transpose data=long out=want;
by id;
var date;
id vname;
run;

In your data, flag seems to always be constant for a given id, is this also true for your real data?

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 927 views
  • 5 likes
  • 4 in conversation