Sujit Patnaik
Sujit Patnaik

Reputation: 35

Creating multiple variables using multiple variables in SAS

The data is like this.....

Line1       Line2       Product_name   Segment 1  Segment 2 

EUCREAS     ORAAL       EUCREAS        DPP-4      DPP-4 -EUCREAS
GALVUS      ORAAL       GALVUS         DPP-4      DPP-4 -GALVUS
GLICLAZIDE  ORAAL       DIAMICRON      SU         SU -GLICLAZIDE
GLICLAZIDE  ORAAL       GLICLAZIDE     SU         SU -GLICLAZIDE
GLP-1       ORAAL       BYDUREON       GLP-1      GLP-1
GLP-1       ORAAL       BYETTA         GLP-1      GLP-1
GLP-1       ORAAL       LYXUMIA        GLP-1      GLP-1
GLP-1       ORAAL       VICTOZA        GLP-1      GLP-1
INSULINES   INSULINES   ACTRAPHANE     INSULINES  INSULINES
INSULINES   INSULINES   ACTRAPID       INSULINES  INSULINES

here I have to use line1,line2 and product_name to create segment1 & segment2.

I can use if statement but are there any other ways to do this in data step or proc sql.

Upvotes: 0

Views: 109

Answers (1)

Chris Long
Chris Long

Reputation: 1319

Assuming you have a fairly simple mapping from LINE1/LINE2/PRODUCT_NAME to your segments, a nice way to do this is to create a format:

proc format;
  value $seg1f
    'EUCREAS:ORAAL:EUCREAS' = 'DPP-4'
    'GALVUS:ORAAL:EUCREAS' = 'DPP-4'
    ....
  ;
  value $seg2f
    'EUCREAS:ORAAL:EUCREAS' = 'DPP-4 - EUCREAS'
    'GALVUS:ORAAL:EUCREAS' = 'DPP-4 - GALVUS'
    ....
  ;
run;

You can then just assign:

data want;
  set have;
  seg1 = put(catx(':', line1, line2, product_name), $seg1f.);
  seg2 = put(catx(':', line1, line2, product_name), $seg2f.);
run;

Or you can use the same formats in a PROC SQL step if that's more convenient. You would need to think carefully about the delimiter to use, and you can't call the formats just seg1 and seg2 because format names aren't allowed to end with digits. You can create the formats programatically if necessary, see the CNTLIN option on PROC FORMAT.

Upvotes: 2

Related Questions