BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
dellerrrr
Fluorite | Level 6

Hello everyone,

 

Currently I am struggling with a problem I cannot seem to resolve. I am trying to see the frequency that different antidepressants are appearing in the dataset along with if there are observations with more than one antidepressant. Every observation will have SubstanceName1-SubstanceName30 and Class1-Class30. Below is some sample data of how it appears in the set. 

 

Sample Data

 

ID#      SubstanceName1      SubstanceClass1     SubstanceName2       SubstanceClass2       

1          Trazodone                  Antidepressant         Fluoxetine                      Antidepressant

2          Heroin                        Opiate                      Trazodone                      Antidepressant              

 

 

Currently the array code I am using below will only return the name of an antidepressant that appears last if there are multiple. Is there any way to count if there are observations with multiple antidepressants like the ID#1, and if so to return both Substance Names? Thank you for any guidance. 

 


Data depress;
set set_1;

Array AntiD $ SubstanceClass1-SubstanceClass30;
Array Name $ SubstanceName1-SubstanceName30;
do i= 1 to 30;
if AntiD(i) = 'Antidepressant' then Depression = Name(i);
end;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

My mistake, how about this

 

Data depress;
    set set_1;
    Array AntiD $ SubstanceClass1-SubstanceClass30;
    Array Name $ SubstanceName1-SubstanceName30;
    count=0;
    do i= 1 to 30;
        if AntiD(i) = 'Antidepressant' then do;
            Depression = Name(i);
            count=count+1;
            output;
        end;
    end;
run;
--
Paige Miller

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

It seems like this will work:

 

Data depress;
    set set_1;
    Array AntiD $ SubstanceClass1-SubstanceClass30;
    Array Name $ SubstanceName1-SubstanceName30;
    count=0;
    do i= 1 to 30;
        if AntiD(i) = 'Antidepressant' then Depression = Name(i);
        count=count+1;
        output;
    end;
run;
--
Paige Miller
dellerrrr
Fluorite | Level 6
PaigeMiller,

Unfortunately this has Depression return as blank,
& count has simply counted from 1 to 30 down the data and then resets at the 31st observation
PaigeMiller
Diamond | Level 26

My mistake, how about this

 

Data depress;
    set set_1;
    Array AntiD $ SubstanceClass1-SubstanceClass30;
    Array Name $ SubstanceName1-SubstanceName30;
    count=0;
    do i= 1 to 30;
        if AntiD(i) = 'Antidepressant' then do;
            Depression = Name(i);
            count=count+1;
            output;
        end;
    end;
run;
--
Paige Miller
dellerrrr
Fluorite | Level 6
Paige Miller,

Awesome! Thank you. Do you have any thoughts on how to return those antidepressant names where there are multiple? I am trying to get a true measure of the distribution of them.

Thank you
Reeza
Super User
Can you restructure your data? If it's in a long form, this is much easier to answer these types of questions.
PaigeMiller
Diamond | Level 26

@dellerrrr wrote:
Paige Miller,

Awesome! Thank you. Do you have any thoughts on how to return those antidepressant names where there are multiple? I am trying to get a true measure of the distribution of them.

Thank you

I don't know what this means. Show me the exact output you want.

--
Paige Miller
dellerrrr
Fluorite | Level 6
I was looking to return the substance names total. So if there is only one match just one name, and if more than return multiple. I tranposed the data like Reeza suggested and have figured it out.

Thank you!
Reeza
Super User
If you're in a long format, SQL will do 'aggregations' on character variables and/or count distinct which can make life a lot easier to figure out the number of distinct drugs a client is using.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 555 views
  • 0 likes
  • 3 in conversation