BookmarkSubscribeRSS Feed
walidqerem
Calcite | Level 5

Hi all

I am trying to restructure my data and any help will be really appreciated. I have attached four screenshots,2016-07-15.png  and 2016-07-15 (1).png show the original data and the rest show how I would like the data to be rearranged. I would do it manually but I have huge data and that would be time consuming 

 


2016-07-15 (1).png2016-07-15 (2).png2016-07-15 (3).png2016-07-15.png
6 REPLIES 6
RahulG
Barite | Level 11

Your problem is not very clear. Please illustrate more so that I can give you solution.

In one png, it is the list of table columns and second one is the actual table. Now I do not understand how do you want to structure your data.

 

 

walidqerem
Calcite | Level 5

Thank you for the quick reply

In the original data I have the following variables Patient Id variable, a string variable that contains a drug nme, drug recieved data, strength, repetition, patient age...etc

Example

Patient ID             Drug name                  recieved date       strength

1                            Cefrriaxone                      1/1/2016        500

1                            Cefrriaxone                      2/1/2016         500

2                             Cefaclor                          5/6/2016      1000

2                              Cefaclor                        6/6/2016       1000

And I wanted restructure the data by transforming each different drug name in the drug name variable to a new variable and transform the date to starting date and discontinuation date (1st and last recieved date for each drug for each patient). so the data would look like this

Patient ID    Ceftriaxone   starting date       disc.date           strength     cefaclor   startingdate2   disc.date2   str.2

1                   1                  1/1/2016             2/1/2016              500          2                                                         n/p

2                   2                                                                         n/a           1              5/6/2016          6/6/2016    1000

Reeza
Super User

How many drugs do you have?

walidqerem
Calcite | Level 5
34
Reeza
Super User

I generally don't recommend this data structure. You'll need to know all the drug names for any future analysis. 

 

If you really want it, you can do three proc transposes and merge results.

 

Another option is to use the macro A better way to flip

http://www.sascommunity.org/wiki/A_Better_Way_to_Flip_(Transpose)_a_SAS_Dataset

Ksharp
Super User
More data would be better.
Check the MERGE skill proposed by Me,Matt,Arthur.T 

http://support.sas.com/resources/papers/proceedings15/2785-2015.pdf





data have;
input PatientID             Drugname   : $20.               recieveddate : $20.      strength;
cards;
1                            Cefrriaxone                      1/1/2016        500
1                            Cefrriaxone                      2/1/2016         500
2                             Cefaclor                          5/6/2016      1000
2                              Cefaclor                        6/6/2016       1000
;
run;
data temp(index=(xx=(PatientID   strength)));
 set have;
 by PatientID   Drugname   strength;
 if first.strength then n=0;
 n+1;
run;
proc sql;
select distinct catt('temp(where=(n=',n,'and PatientID=',PatientID,'and strength=',strength,') 
rename=(recieveddate=',cats(Drugname,'Date',n),'))')
into : merge separated by ' '
 from temp;
quit;
data want;
 merge &merge;
 by PatientID   strength;
 output;
 call missing(of _all_);
 drop Drugname   n;
run;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 6 replies
  • 1303 views
  • 0 likes
  • 4 in conversation