1. Use PROC TRANSPOSE so the variables are named properly.
2. Use a shortcut reference to your variables health with the colon after acts as a wildcard. health_: will reference all variables that start with health. You'll need to use arrays for this.
array _myvars(*) health_:; *includes all variables that start with health_ ;
Here's a tutorial on using Arrays in SAS
https://stats.idre.ucla.edu/sas/seminars/sas-arrays/
Here is a reference that illustrates how to refer to variables and datasets in a short cut list:
https://blogs.sas.com/content/iml/2018/05/29/6-easy-ways-to-specify-a-list-of-variables-in-sas.html
Having a list of variables with common prefix and a suffix as serial numbers
like:
health_1 health_2 health_3 health_4 health_5 health_6 health_7 health_8 health_9 health_10 health_11 health_12.
you can shorten it to health_1 - health_12;
In some sas functions you can use the abbreviation health_: - to relate to all variables starting with health_, doesn't matter how many variables there are with this prefix or what suffixes they have.
In case you are checking just one value, it is preferred to check:
if health_n = 1 instead if health_n in(1).
Instead writing:
if health_1 in (1) or health_2 in (1) or health_3 in (1) or health_4 in (1) health_5 in (1) or health_6 in (1) or health_7 in (1) or health_8 in (1) or health_9 in (1) or health_10 in (1) health_11 in (1) or health_12 in (1) then health=1;
you can use next code:
array hl {*} health_1 - health_12;
health = 1;
do i=1 to dim(hl);
if hl(i) ne 1 then health = 0;
end;
that means that if any of the health conditions is not 1 then the final flag health is 0;
I hope that satisfies what you look for.
You asked for "Can you please suggest what is the wrong with the following code?" -
in such case it is better to post the log, using the </> icon.
As &hlth is a list of values you need change next line, from
if hl(i) ne &hlth then health_ = 0;
into:
if hl(i) in &hlth then health_ = 0;
without a log I cannot say if there are other issues.
At start few remarks:
1) Post code using the "running men" icon, in order to show indent and color the code, to distinguish easilly between functions, keywards and literals.
2) Though there is no error message in log, sometimes warnings may help and point at issues.
3) The statement
%let hlth = %nrstr('10029104');
assigns just one value to &hlth, then why assigning it to an ARRAY?
there is no meaning of using IN () or whichc() -
the whole code is not clear what you want to do.
4) As it is a following post to a solved one, please rewrite this query as a new one.
Thanks for your suggestion and time. I will follow your instructions in future posts. I just wanted to inform that I edited the last code to which you already replied. Please ignore it as it will be also posted as a new topic as you suggested. thanks.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.