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

Hi guys, 

suppose to have the following: 

 

data DB;
  input ID :$20. Admission :date09. Discharge :date09. Class Value; 
  format Admission date9. Discharge date9.;
cards;
0001 13JAN2015 20JAN2015 2  0.32
0001 21FEB2015 31DEC2015 2  0.32
0001 01MAR2018 30SEP2018 3  0.43
0001 01JAN2019 31DEC2019 3  0.43
0002 01JAN2015 31DEC2015 2  1.92
0002 01JAN2019 31OCT2019 2  1.92
0003 08FEB2014 10MAR2014 1  4.32
0003 16JUN2015 13JUL2015 1  4.32
0004 04MAY2016 10MAY2016 3  3.22
0004 13SEP2017 15NOV2017 3  3.22
0004 09DEC2018 31DEC2018 3  3.22
;

Is there a way to get the following? 

 


data DB1;
  input ID :$20. Admission :date09. Discharge :date09. Class Class1 Class2 Class3 Class4; 
  format Admission date9. Discharge date9.;
cards;
0001 13JAN2015 20JAN2015 2   .   0.32   .     .
0001 21FEB2015 31DEC2015 2   .   0.32   .     .
0001 01MAR2018 30SEP2018 3   .     .   0.43   .
0001 01JAN2019 31DEC2019 3   .     .   0.43   .
0002 01JAN2015 31DEC2015 2   .   1.92   .     .
0002 01JAN2019 31OCT2019 2   .   1.92   .     .
0003 08FEB2014 10MAR2014 1  4.32   .    .     .
0003 16JUN2015 13JUL2015 1  4.32   .    .     .
0004 04MAY2016 10MAY2016 3   .     .  3.22    .
0004 13SEP2017 15NOV2017 3   .     .  3.22    .
0004 09DEC2018 31DEC2018 3   .     .  3.22    .
;

In other words I would like to "distribute" the values of variable "Value" across all classes in Class variable. 

 

Thank you in advance

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

This will work for the example data:

data work.db1;
   set work.db;
   array c (*) class1-class4;
   c[class]=value;
   drop value;
run;

Arrays are a way of referencing, usually related, variables using one or more numeric index variables. The ARRAY statement creates the reference. Because you already have a variable named Class we couldn't use that as a name so list the names you wanted. The index is placed in parentheses after the array name. I use the [ ] as a style choice to remind myself this is an array.

Arrays can only hold variables of the same type, no mixing numeric and character.

View solution in original post

2 REPLIES 2
ballardw
Super User

This will work for the example data:

data work.db1;
   set work.db;
   array c (*) class1-class4;
   c[class]=value;
   drop value;
run;

Arrays are a way of referencing, usually related, variables using one or more numeric index variables. The ARRAY statement creates the reference. Because you already have a variable named Class we couldn't use that as a name so list the names you wanted. The index is placed in parentheses after the array name. I use the [ ] as a style choice to remind myself this is an array.

Arrays can only hold variables of the same type, no mixing numeric and character.

Ksharp
Super User
data DB;
  input ID :$20. Admission :date09. Discharge :date09. Class Value; 
  format Admission date9. Discharge date9.;
cards;
0001 13JAN2015 20JAN2015 2  0.32
0001 21FEB2015 31DEC2015 2  0.32
0001 01MAR2018 30SEP2018 3  0.43
0001 01JAN2019 31DEC2019 3  0.43
0002 01JAN2015 31DEC2015 2  1.92
0002 01JAN2019 31OCT2019 2  1.92
0003 08FEB2014 10MAR2014 1  4.32
0003 16JUN2015 13JUL2015 1  4.32
0004 04MAY2016 10MAY2016 3  3.22
0004 13SEP2017 15NOV2017 3  3.22
0004 09DEC2018 31DEC2018 3  3.22
;
proc sort data=db;
by id Admission Discharge class;
run;
proc transpose data=db prefix=class out=want(drop=_name_);
by id Admission Discharge class;
var value;
id class;
run;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 300 views
  • 3 likes
  • 3 in conversation