SAS Macros Help - Generating IF ELSE Conditions dynamically using macros

Reply
New Contributor
Posts: 2

SAS Macros Help - Generating IF ELSE Conditions dynamically using macros

Dear All,

       I am facing the problem here. I am using sas 9.2 and would like to build a dataset in the following way.

I have a table called rules which has variable Name, and its lower and upper bound based classifications

eg.

age 1   20 Child

age 20 40 Youth

age 40 100 Oldies

height.....

etc...

Now i want to run this on a base data, and create a separate group variable which will indicate whether they are children Youth or Oldies.

I am stuck at this for quite sometime now trying to use macros to generate the if conditions, but end up getting a this statement is not valid or out of order as error.

Thanks in advance for the help.

CHeers

Sudharshan

Super Contributor
Posts: 349

Re: SAS Macros Help - Generating IF ELSE Conditions dynamically using macros

Hi,

Is this what you want..

data rules;

input lbound hbound desc $;

cards;

1  20 Child

20 40 Youth

40 100 Oldies

;

run;

data fmt (keep=FMTNAME START END LABEL TYPE);

length FMTNAME $30. START END $256.;

set WORK.rules;

      FMTNAME = 'age' ;

      START   = lbound;

      END     = hbound;

      LABEL   = desc;

      TYPE    = 'N' ;

run;

proc format cntlin=fmt lib=work; run;

data basetable;

input age;

desc=put(age,age.);

cards;

24

45

22

99

11

;

run;

Thanks,

Shiva

Frequent Contributor
Posts: 95

Re: SAS Macros Help - Generating IF ELSE Conditions dynamically using macros

Without seeing your code it is impossible to know why you get the above mentioned error.

An alternative way could be using user-defined formats to assign labels(Ages 0 or greater than 100 are not accounted for).

Zafer

proc format;

    value age

    1 -< 20 = 'Child' /* [1,20) */

    20 -< 40 = 'Youth' /* [20,40) */

    40 - 100 = 'Oldies' /* [40,100] */;

run;

data test;

    input Age;

    Group = PUT(Age,age.);

    put (Age Group) (=);

datalines;

1

19

20

39

40

100

;

run;

Age=1 Group=Child

Age=19 Group=Child

Age=20 Group=Youth

Age=39 Group=Youth

Age=40 Group=Oldies

Age=100 Group=Oldies

New Contributor
Posts: 2

Re: SAS Macros Help - Generating IF ELSE Conditions dynamically using macros

Thanks a lot guys

I didn't know of proc format previously, and this definitely seems to be a better option than the previous idea of mine

Ask a Question
Discussion stats
  • 3 replies
  • 222 views
  • 0 likes
  • 3 in conversation