## base sas

Super Contributor
Posts: 323

# base sas

I just posted a the following question. There are few mistakes in previous one. I am posting again.

I am creating a new variable 'AESERN' from the information contained in 7 variables.

the 7 variables are;

AESER    AESDTH      AESLIFE     AESHOSP      AESCONG       AESDISAB      AESMIE        INFECDIS

N                N                 N                    N                N                        N                  N                    N

Y               N                  N                   N                N                         N                 N                     N

Y                N                  N                    N               N                          N                Y                     N

Y                 Y                  Y                     N              N                           N               N                     N

SPECS shows: if AESER = 'N' THEN AESERN = 'No';
if AESER = 'Y' THEN AESERN = 'Yes';
if AESDTH OR AESLIFE OR AESHOSP OR AESCONG OR AESDISAB OR AESMIE OR INFECDIS are 'Y' then append " (" to AESERN and then display the number(s) separated by commas "," of the conditions met. Follow all numeric code values with ")".
IF AESDTH = 'Y' THEN APPEND 1.
IF AESLIFE = 'Y' THEN APPEND 2.
IF AESHOSP = 'Y' THEN APPEND 3.
IF AECONG = 'Y' THEN APPEND 5.
IF AESDISAB = 'Y' THEN APPEND 4.
IF AESMIE = 'Y' THEN APPEND 6.
IF INFECDIS = 'Y' THEN APPEND 7.
As an example, if both AESDTH and INFECDIS are marked, then AESERN woul have value "Yes (1,6)".

My code:

if AESER = 'N' THEN AESERN = 'No';
array AESP {7} AESDTH AESLIFE AESHOSP AESCONG AESDISAB AESMIE INFECDIS;
do i = 1 to 7;
if AESER = 'Y' and array (i) = 'N' then AESERN = 'Yes';
if AESER = 'Y' and array (i) = 'Y' then AESERN = 'Yes'||'('||i||')';
end;

Super Contributor
Posts: 323

## base sas

I am creating a new variable 'AESERN' from the information contained in 7 variables.

the 7 variables are; AESER    AESDTH      AESLIFE     AESHOSP      AESCONG       AESDISAB      AESMIE        INFECDIS

N                N                 N                    N                N                        N                  N                    N

Y               N                  N                   N                N                         N                 N                     N

Y                N                  N                    N               N                          N                Y                     N

Y                 Y                  Y                     N              N                           N               N                     N

SPECS shows: if AESER = 'N' THEN AESERN = 'No';
if AESER = 'Y' THEN AESERN = 'Yes';
if AESDTH OR AESLIFE OR AESHOSP OR AESCONG OR AESDISAB OR AESMIE OR INFECDIS are 'Y' then append " (" to AESERN and then display the number(s) separated by commas "," of the conditions met. Follow all numeric code values with ")".
IF AESDTH = 'Y' THEN APPEND 1.
IF AESLIFE = 'Y' THEN APPEND 2.
IF AESHOSP = 'Y' THEN APPEND 3.
IF AECONG = 'Y' THEN APPEND 5.
IF AESDISAB = 'Y' THEN APPEND 4.
IF AESMIE = 'Y' THEN APPEND 6.
IF INFECDIS = 'Y' THEN APPEND 7.
As an example, if both AESDTH and INFECDIS are marked, then AESERN woul have value "Yes (1,6)".

My code:

if AESER = 'N' THEN AESERN = 'No';
array AESP {7} AESDTH AESLIFE AESHOSP AESCONG AESDISAB AESMIE INFECDIS;
do i = 1 to 7;
if AESER = 'Y' and array (i) = 'N' then AESERN = 'Yes';
if AESER = 'Y' and array (i) = 'Y' then AESERN = 'Yes'||'('||i||')';

end;

Super User
Posts: 13,583

## Re: base sas

You should provide more example of what the outpu should look like. Also, how will you use that AESERN variable?

You may very well have problems if your variable AESERN already exists as the existing length for AESERN will limit what you may get for a result with the concatenation of values. If it doesn't exist you need to assign a value long enough all of the characters you expect.

You are also referencing the array incorrectly ;

if AESER = 'Y' and array (i) = 'N' then AESERN = 'Yes';

should be

if AESER = 'Y' and AESP (i) = 'N' then AESERN = 'Yes';

with your requirement to wrap those i values within () you will need a bit more logic

``````
data want;
set have;
length aesern \$ 25 tstr \$ 15;
if AESER = 'N' THEN AESERN = 'No';
array AESP {7} AESDTH AESLIFE AESHOSP AESCONG AESDISAB AESMIE INFECDIS;
do i = 1 to 7;
if AESER = 'Y' and AESP (i) = 'N' then tstr=catx(',',tstr,i);
end;
/* you only need to assing the YES part if any were found before*/
if lengthn (tstr)>0 then Aesern= catt('Yes (',tstr,')');
drop tstr;
run;
``````
Super User
Posts: 13,583