Hi,
I have a table "basedata" with 2 variables, birthweight & gestage.
I have a macro "sga" that determines whether or not each baby is small based on the birthweight & gestage. It created another variable called sgavar.
I was wondering how to invoke the macro to create a new table w/ the new variable.
Without the macro it's almost impossible to say.
%MACRO sga(bwvar,gadaysvar,sgavar);
IF &bwvar > . AND &gadaysvar > . THEN DO;
&gadaysvar = &gadaysvar -3.5;
IF 19*7 < &gadaysvar <= 20*7 THEN DO;
IF &bwvar <=275 THEN &sgavar =1;
ELSE IF &bwvar >=772 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 20*7 < &gadaysvar <=21*7 THEN DO;
IF &bwvar <=314 THEN &sgavar =1;
ELSE IF &bwvar >=790 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 21*7 < &gadaysvar <=22*7 THEN DO;
IF &bwvar <=376 THEN &sgavar =1;
ELSE IF &bwvar >=826 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 22*7 < &gadaysvar <=23*7 THEN DO;
IF &bwvar <=440 THEN &sgavar =1;
ELSE IF &bwvar >=882 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 23*7 < &gadaysvar <=24*7 THEN DO;
IF &bwvar <=498 THEN &sgavar =1;
ELSE IF &bwvar >=977 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 24*7 < &gadaysvar <=25*7 THEN DO;
IF &bwvar <=558 THEN &sgavar =1;
ELSE IF &bwvar >=1138 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 25*7 < &gadaysvar <=26*7 THEN DO;
IF &bwvar <=625 THEN &sgavar =1;
ELSE IF &bwvar >=1362 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 26*7 < &gadaysvar <=27*7 THEN DO;
IF &bwvar <=702 THEN &sgavar =1;
ELSE IF &bwvar >=1635 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 27*7 < &gadaysvar <=28*7 THEN DO;
IF &bwvar <=798 THEN &sgavar =1;
ELSE IF &bwvar >=1977 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 28*7 < &gadaysvar <=29*7 THEN DO;
IF &bwvar <=925 THEN &sgavar =1;
ELSE IF &bwvar >=2361 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 29*7 < &gadaysvar <=30*7 THEN DO;
IF &bwvar <=1085 THEN &sgavar =1;
ELSE IF &bwvar >=2710 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 30*7 < &gadaysvar <=31*7 THEN DO;
IF &bwvar <=1278 THEN &sgavar =1;
ELSE IF &bwvar >=2986 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 31*7 < &gadaysvar <=32*7 THEN DO;
IF &bwvar <=1495 THEN &sgavar =1;
ELSE IF &bwvar >=3200 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 32*7 < &gadaysvar <=33*7 THEN DO;
IF &bwvar <=1725 THEN &sgavar =1;
ELSE IF &bwvar >=3370 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 33*7 < &gadaysvar <=34*7 THEN DO;
IF &bwvar <=1950 THEN &sgavar =1;
ELSE IF &bwvar >=3502 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 34*7 < &gadaysvar <=35*7 THEN DO;
IF &bwvar <=2159 THEN &sgavar =1;
ELSE IF &bwvar >=3596 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 35*7 < &gadaysvar <=36*7 THEN DO;
IF &bwvar <=2354 THEN &sgavar =1;
ELSE IF &bwvar >=3668 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 36*7 < &gadaysvar <=37*7 THEN DO;
IF &bwvar <=2541 THEN &sgavar =1;
ELSE IF &bwvar >=3755 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 37*7 < &gadaysvar <=38*7 THEN DO;
IF &bwvar <=2714 THEN &sgavar =1;
ELSE IF &bwvar >=3867 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 38*7 < &gadaysvar <=39*7 THEN DO;
IF &bwvar <=2852 THEN &sgavar =1;
ELSE IF &bwvar >=3980 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 39*7 < &gadaysvar <=40*7 THEN DO;
IF &bwvar <=2929 THEN &sgavar =1;
ELSE IF &bwvar >=4060 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 40*7 < &gadaysvar <=41*7 THEN DO;
IF &bwvar <=2948 THEN &sgavar =1;
ELSE IF &bwvar >=4094 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 41*7 < &gadaysvar <=42*7 THEN DO;
IF &bwvar <=2935 THEN &sgavar =1;
ELSE IF &bwvar >=4098 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 42*7 < &gadaysvar <=43*7 THEN DO;
IF &bwvar <=2907 THEN &sgavar =1;
ELSE IF &bwvar >=4098 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 43*7 < &gadaysvar <=44*7 THEN DO;
IF &bwvar <=2885 THEN &sgavar =1;
ELSE IF &bwvar >=4098 THEN &sgavar =3;
ELSE &sgavar =2;
END;
&gadaysvar = &gadaysvar +3.5;
END;
%MEND sga;
Execute the macro before something like:
data want;
set have;
%sga(firstvarname, secondvar, lastvar); /*firstvarname, secondvar, lastvar are the names of variables in the HAVE data set*/
run;
And if any of this is supposed to be processing data by week and days of year then it is time to hit someone over the head and learn proper date functions and values.
@ballardw wrote:
Execute the macro before something like:
data want;
set have;
%sga(firstvarname, secondvar, lastvar); /*firstvarname, secondvar, lastvar are the names of variables in the HAVE data set*/
run;
And if any of this is supposed to be processing data by week and days of year then it is time to hit someone over the head and learn proper date functions and values.
And Formats.
Your macro would have to be run inside a DATA step where the variables bwvar, gadaysvar, sgavar exist:
data want;
set have;
%sga(bwvar,gadaysvar,sgavar);
run;
I'm getting all null values for sgavar, which I know shoudn't be the case. Not sure what's going on...
Post your code and log.
Make sure the variable names are exactly as they should be.
proc sql;
create table test as select
bwgt as bwvar
,gadays as gadaysvar
from basedata
;
quit;
%MACRO sga(bwvar,gadaysvar,sgavar);
IF &bwvar > . AND &gadaysvar > . THEN DO;
&gadaysvar = &gadaysvar -3.5;
IF 19*7 < &gadaysvar <= 20*7 THEN DO;
IF &bwvar <=275 THEN &sgavar =1;
ELSE IF &bwvar >=772 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 20*7 < &gadaysvar <=21*7 THEN DO;
IF &bwvar <=314 THEN &sgavar =1;
ELSE IF &bwvar >=790 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 21*7 < &gadaysvar <=22*7 THEN DO;
IF &bwvar <=376 THEN &sgavar =1;
ELSE IF &bwvar >=826 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 22*7 < &gadaysvar <=23*7 THEN DO;
IF &bwvar <=440 THEN &sgavar =1;
ELSE IF &bwvar >=882 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 23*7 < &gadaysvar <=24*7 THEN DO;
IF &bwvar <=498 THEN &sgavar =1;
ELSE IF &bwvar >=977 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 24*7 < &gadaysvar <=25*7 THEN DO;
IF &bwvar <=558 THEN &sgavar =1;
ELSE IF &bwvar >=1138 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 25*7 < &gadaysvar <=26*7 THEN DO;
IF &bwvar <=625 THEN &sgavar =1;
ELSE IF &bwvar >=1362 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 26*7 < &gadaysvar <=27*7 THEN DO;
IF &bwvar <=702 THEN &sgavar =1;
ELSE IF &bwvar >=1635 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 27*7 < &gadaysvar <=28*7 THEN DO;
IF &bwvar <=798 THEN &sgavar =1;
ELSE IF &bwvar >=1977 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 28*7 < &gadaysvar <=29*7 THEN DO;
IF &bwvar <=925 THEN &sgavar =1;
ELSE IF &bwvar >=2361 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 29*7 < &gadaysvar <=30*7 THEN DO;
IF &bwvar <=1085 THEN &sgavar =1;
ELSE IF &bwvar >=2710 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 30*7 < &gadaysvar <=31*7 THEN DO;
IF &bwvar <=1278 THEN &sgavar =1;
ELSE IF &bwvar >=2986 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 31*7 < &gadaysvar <=32*7 THEN DO;
IF &bwvar <=1495 THEN &sgavar =1;
ELSE IF &bwvar >=3200 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 32*7 < &gadaysvar <=33*7 THEN DO;
IF &bwvar <=1725 THEN &sgavar =1;
ELSE IF &bwvar >=3370 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 33*7 < &gadaysvar <=34*7 THEN DO;
IF &bwvar <=1950 THEN &sgavar =1;
ELSE IF &bwvar >=3502 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 34*7 < &gadaysvar <=35*7 THEN DO;
IF &bwvar <=2159 THEN &sgavar =1;
ELSE IF &bwvar >=3596 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 35*7 < &gadaysvar <=36*7 THEN DO;
IF &bwvar <=2354 THEN &sgavar =1;
ELSE IF &bwvar >=3668 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 36*7 < &gadaysvar <=37*7 THEN DO;
IF &bwvar <=2541 THEN &sgavar =1;
ELSE IF &bwvar >=3755 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 37*7 < &gadaysvar <=38*7 THEN DO;
IF &bwvar <=2714 THEN &sgavar =1;
ELSE IF &bwvar >=3867 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 38*7 < &gadaysvar <=39*7 THEN DO;
IF &bwvar <=2852 THEN &sgavar =1;
ELSE IF &bwvar >=3980 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 39*7 < &gadaysvar <=40*7 THEN DO;
IF &bwvar <=2929 THEN &sgavar =1;
ELSE IF &bwvar >=4060 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 40*7 < &gadaysvar <=41*7 THEN DO;
IF &bwvar <=2948 THEN &sgavar =1;
ELSE IF &bwvar >=4094 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 41*7 < &gadaysvar <=42*7 THEN DO;
IF &bwvar <=2935 THEN &sgavar =1;
ELSE IF &bwvar >=4098 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 42*7 < &gadaysvar <=43*7 THEN DO;
IF &bwvar <=2907 THEN &sgavar =1;
ELSE IF &bwvar >=4098 THEN &sgavar =3;
ELSE &sgavar =2;
END;
ELSE IF 43*7 < &gadaysvar <=44*7 THEN DO;
IF &bwvar <=2885 THEN &sgavar =1;
ELSE IF &bwvar >=4098 THEN &sgavar =3;
ELSE &sgavar =2;
END;
&gadaysvar = &gadaysvar +3.5;
END;
%MEND sga;
data want;
set test;
%sga(bwvar,gadaysvar,sgavar);
run;
thanks for everyone's help!
was using the wrong variable -__-
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!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.