DATA Step, Macro, Functions and more

base sas

Reply
Super Contributor
Posts: 272

base sas

Dear sir/madam,

 

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

 

I am having little trouble in my code . Please help.

 

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;

 

This code is not giving me output. Please help. Thanks.

Super Contributor
Posts: 272

base sas

Posted in reply to knveraraju91

Dear sir/madam,

 

I am havinh little trouble in my code . Please help.

 

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||')';

 

This code giving me output. Please help. Thanks.
end;

Super User
Posts: 11,343

Re: base sas

Posted in reply to knveraraju91

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: 11,343

Re: base sas

Posted in reply to knveraraju91

Please do not create duplicate posts, especially in the same section of the forum. If you need to change something you can edit your post. Multiple posts get confusing as parts of answers, or your responses for details end up in different threads and the final result may not be cohesive.

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