[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1982
  • Last Modified:

SAS Rows to Columns

I need to do the following in "SAS"  (Also the number of Meds will vary. So there might be 3, 10, 20, etc)
Take the example dataset from
ID Med
1    A  
1    B
1    C
2    D
3    A
3    C


To
ID   Med_1   Med_2   Med_3  
1      A             B         C
2      D
3      A             C


Thanks,
J
0
flyfishin4trout
Asked:
flyfishin4trout
  • 2
1 Solution
 
wigmeisterCommented:
Use Proc Transpose

I would change your ID variable to something else, since ID is a SAS keyword.  I used new_id here.

proc transpose data=test1 out=test2 prefix=med_;
by new_id;
ID new_id;
var med;
run;

May not be 100% accurate, but this is basically what you need.

Here are a couple of links that may help:
analytics.ncsu.edu/sesug/2005/TU12_05.PDF
www2.sas.com/proceedings/sugi29/267-29.pdf
0
 
Kumar1505Commented:
Hi J

Here is the answer. Its simple code and exact solution to your problem. It creates "mid_" columns based on the values of ID column. If ID column has 10 unique values then there will be 10 "mid_" variables in the result dataset.

Regards
Kumar
proc transpose data=sample out=result prefix=mid_;
by id;
var med;
run;
 
If the dataset "sample" is not sorted:
 
proc sort data=sample;
by id;
run;
 
proc transpose data=sample out=result prefix=mid_;
by id;
var med;
run;

Open in new window

0
 
wigmeisterCommented:
Proc transpose is definitely what he needed as was my suggestion.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now