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

Hi ,

 

If I want to change the data set from A to B ,what is the way.

Data set A

Risk Risk class Risk plan  Key
1 ART 1 A
1 ART 3 B
1 ART 5 C
2 SCH 5 C
1 SCH 1 D
1 ART 3 E
2 SCH 3 E

 

B

 

Key Risk1 Risk2 Risk class1 Risk class2 Risk plan1 Risk Plan2
A 1   ART   1  
B 1 2 ART   3  
C 1   ART SCH 5 5
D 1   SCH   1  
E 1 2 ART SCH 3 3

 

the key thing is here risk is not a constant for  a max of 2 for each key,it can have any number for each key ..

 

for ex: key A has  1 risk value.

           key B  has 1 risk value

           key  C has 2 risk values

           key  D has 1 risk value

           key E has 2 risk values

           Key F can have 3 risk values.

 

I can do with arrays but I know if the risk value is max 2 or fixed  then it can be  done.but i am not sure how to achieve if the risk max number is not fixed.

 

Thanks in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Then you run the double transpose without the extra counter, but by using risk as ID variable:

data have;
infile datalines dlm="09"x dsd;
input risk risk_class $ risk_plan $ key $;
datalines;
1	ART	1	A
1	ART	3	B
1	ART	5	C
2	SCH	5	C
1	SCH	1	D
1	ART	3	E
2	SCH	3	E
2	ART	1	F
;

proc transpose data=have out=long;
by key risk;
var risk risk_class risk_plan;
run;

proc transpose data=long out=want (drop=_name_);
by key;
var col1;
id _name_ risk;
run;

View solution in original post

6 REPLIES 6
andreas_lds
Jade | Level 19

Calling proc transpose for each variable (risk, risk_class and risk_plan) and merging the results (all by key), seems to be an easy way to transform the dataset into a report-like structure.

 

proc transpose data= have out=Risk(drop= _name_) prefix=Risk;
   by Key;
   var Risk;
run;

proc transpose data= have out=Risk_Class(drop= _name_) prefix=Risk_Class;
   by Key;
   var Risk_Class;
run;

proc transpose data= have out=Risk_Plan(drop= _name_) prefix=Risk_Plan;
   by Key;
   var Risk_Plan;
run;

data want;
   merge Risk Risk_Class Risk_Plan;
   by Key;
run;
sfffdg
Obsidian | Level 7

Thanks a lot for the solution .

I find it is working fine except for one scenario. ( For key 'F')

swathiprasad_2-1633592427643.png

 



excepted result:

swathiprasad_0-1633592271079.png

 


but actual result;

swathiprasad_1-1633592368141.png

 

how can we get rid of this problem?

Kurt_Bremser
Super User

Create a counter, and run a double PROC TRANSPOSE:

data have;
infile datalines dlm="09"x dsd;
input risk risk_class $ risk_plan $ key $;
datalines;
1	ART	1	A
1	ART	3	B
1	ART	5	C
2	SCH	5	C
1	SCH	1	D
1	ART	3	E
2	SCH	3	E
2	ART	1	F
;

data pretrans;
set have;
by key;
if first.key
then ct = 1;
else ct + 1;
run;

proc transpose data=pretrans out=long;
by key ct;
var risk risk_class risk_plan;
run;

proc transpose data=long out=want (drop=_name_);
by key;
var col1;
id _name_ ct;
run;

You can also run PROC REPORT on pretrans:

proc report data=pretrans;
column key ct,(risk risk_class risk_plan) n;
define key / group;
define ct / "" across;
define risk / display;
define risk_class / display;
define risk_plan / display;
define n / noprint;
run;
sfffdg
Obsidian | Level 7

Thanks a lot for the solution Kurt.

 

But it also gives the same result as andreas_ids.


actual result:

swathiprasad_0-1633600700547.png

expected result is:

swathiprasad_1-1633600747145.png

 

Kurt_Bremser
Super User

Then you run the double transpose without the extra counter, but by using risk as ID variable:

data have;
infile datalines dlm="09"x dsd;
input risk risk_class $ risk_plan $ key $;
datalines;
1	ART	1	A
1	ART	3	B
1	ART	5	C
2	SCH	5	C
1	SCH	1	D
1	ART	3	E
2	SCH	3	E
2	ART	1	F
;

proc transpose data=have out=long;
by key risk;
var risk risk_class risk_plan;
run;

proc transpose data=long out=want (drop=_name_);
by key;
var col1;
id _name_ risk;
run;
Ksharp
Super User
data have;
infile datalines dlm="09"x dsd;
input risk risk_class $ risk_plan $ key $;
datalines;
1	ART	1	A
1	ART	3	B
1	ART	5	C
2	SCH	5	C
1	SCH	1	D
1	ART	3	E
2	SCH	3	E
2	ART	1	F
;

proc sql noprint;
select max(n) into : n from (select key,count(*) as n from have group by key);
quit;
proc summary data=have;
by key;
output out=want idgroup(out[&n] (risk risk_class risk_plan)=);
run;
proc print;run;

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
  • 6 replies
  • 1745 views
  • 1 like
  • 4 in conversation