Hello programmers,
Please can someone summarize what this code is doing? I'm new to SAS macro but this code has been confusing a little bit. I understand that it's doing an array over 16 items
%macro jorm (base, better, worse, first, y, raw);
data jorm_&y; set raw.&raw (keep = hhid pn &base &better &worse);
array base [16] &base;
array better [16] &better;
array worse [16] &worse;
array jorm [16] jorm_&y._1 - jorm_&y._16;
if &first NE . then do;
iqcode_dkrf_&y = 0;
do i = 1 to 16;
if base[i] = 1 then do; /*better*/
if better[i] = 1 then jorm[i] = 1; /*much better*/
else if better[i] in (2, 8, 9) then jorm[i] = 2; /*a bit better*/
end;
else if base[i] = 2 then jorm[i] = 3; /*same*/
else if base[i] = 3 then do; /*worse*/
if worse[i] in (4, 8, 9) then jorm[i] = 4; /*a bit worse*/
else if worse[i] = 5 then jorm[i] = 5; /*much worse*/
end;
else if base[i] in (8, 9) then do; /*8=dk/na, 9=rf*/
jorm[i] = .;
iqcode_dkrf_&y = iqcode_dkrf_&y + 1; /*count of dk/na*/
end;
else if base[i] = 4 then do; /*4=NotApplicable - do NOT count as dk/rf*/
jorm[i] = .;
end;
end;
IQCODE_&y = mean (of jorm[*]); /*mean IQCODE score over non-missing items*/
if iqcode_dkrf_&y > 3 then IQCODE_&y = .; /* set to missing 4+ dk/rf*/
end;
. Please I'll appreciate straight contributions, any referral will not help at this point. Thanks
What's between %macro jnorm.... and %mend ... is your macro definition. You then call this macro somewhere in code like %jnorm(<and here the parameter passing>).
base, better etc. are the parameter names.
For example in your macro definition you've got
- %macro jnorm (base,...
- In the code you've got array base [16] &base;
When you call the macro you pass a value like: %jnorm(myVar1, ...
The macro then executes and replaces all macro variables with the parameter value you've passed.
- array base [16] myVar1;
First the SAS macro language resolves and then "normal" SAS executes the code that has been generated.
If you have a working call to the macro then you can capture the generated SAS code using the MFILE option. After that it's just about understanding "normal" SAS code.
https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/p0j3zpinabl49ln13q5f5r1t46mg.htm
filename mprint '<path of your choosing>/jnorm_gen.sas'; options MPRINT MFILE; %jnorm(...and here comma separated list of parameter values);
What's between %macro jnorm.... and %mend ... is your macro definition. You then call this macro somewhere in code like %jnorm(<and here the parameter passing>).
base, better etc. are the parameter names.
For example in your macro definition you've got
- %macro jnorm (base,...
- In the code you've got array base [16] &base;
When you call the macro you pass a value like: %jnorm(myVar1, ...
The macro then executes and replaces all macro variables with the parameter value you've passed.
- array base [16] myVar1;
First the SAS macro language resolves and then "normal" SAS executes the code that has been generated.
If you have a working call to the macro then you can capture the generated SAS code using the MFILE option. After that it's just about understanding "normal" SAS code.
https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/p0j3zpinabl49ln13q5f5r1t46mg.htm
filename mprint '<path of your choosing>/jnorm_gen.sas'; options MPRINT MFILE; %jnorm(...and here comma separated list of parameter values);
Thanks.
Do you by chance know what this code is doing?
if &first NE . then do;
In the macro
%macro jorm (base, better, worse, first, y, raw);
I know base, better and worse are in the do loop, and are used to create Jorm scores.
y is the wave year (1990-2018)
raw is the raw dataset.
What is the "first" in the macro doing?
&first is a variable most likley.
It does matter how it's defined in the macro call though, so &first is coming from the call which would translate to:
if someVariable is not missing then do.....
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.