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

I am very new to SAS and would appreciate patience and help.

 

I have a data set containing student id, term and audit_type. Each term has 2 audit_types and the student can be present at either of them or be present at both. I need to create a flag for each of these 3 scenarios for each student id each term: 1) if the student is present at only audit_type_1, 2) if s/he is present at only audit_type_2 and 3) if s/he is present at audit_type_1 and audit_type_2 both during that term.

 

**Sample data**
---------------

*Id*            *Term*               *Audit_type*
1                 Fall 2015              1
1                 Fall 2015              2
2                 Winter 2016         1
3                 Winter 2016         2
4                 Spring 2016         1
4                 Spring 2016         2

 

I was able to create a flag for the first 2 scenarios using case when as seen below:

 

proc sql;

create table test as
select id, term, audit_type,
case
when audit_type in ('audit_type_1') then 1
when audit_type in ('audit_type_2 ') then 2
end as audit_type_flag
from have;

 

I can't figure out how to flag the third scenario. So, I want something like below:

 

*Id*            *Term*                  *Audit_type*           *Flag*   
1                 Fall 2015              1                             3
1                 Fall 2015              2                             3
2                 Winter 2016         1                             1
3                 Winter 2016         2                             2
4                 Spring 2016         1                             3
4                 Spring 2016         2                             3

 

All help will be highly appreciated. Thanks in advance for your help and support.

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

It gets simpler for the edited version of your question:

 

proc sql;

create table test as
select 
    id, 
    term, 
    audit_type,
    sum(distinct audit_type) as audit_type_flag
from have
group by id, term;

quit;
PG

View solution in original post

8 REPLIES 8
PGStats
Opal | Level 21

You can get the flag value by summing the audit_type numbers :

 

proc sql;

create table test as
select 
    id, 
    term, 
    audit_type,
    sum(input(scan(audit_type, 3, "_"), 1.)) as audit_type_flag
from have
group by id;

quit;

(untested)

 

PG
PGStats
Opal | Level 21

It gets simpler for the edited version of your question:

 

proc sql;

create table test as
select 
    id, 
    term, 
    audit_type,
    sum(distinct audit_type) as audit_type_flag
from have
group by id, term;

quit;
PG
PDevi
Fluorite | Level 6
The code returns only blanks.
PeterClemmensen
Tourmaline | Level 20

To be sure to get a usable code answer, include sample data in the form of a data step. @PGStatss solution is good. Run the code below. It generates your desired data set.

 

data have;
length Term $20;
infile datalines dlm=',';
input Id $ Term $ Audit_type;
datalines;
1,Fall 2015,1
1,Fall 2015,2
2,Winter 2016,1
3,Winter 2016,2
4,Spring 2016,1
4,Spring 2016,2
;

proc sql;
   create table want as
   select 
       id, 
       term, 
       audit_type,
       sum(distinct audit_type) as audit_type_flag
   from have
   group by id, term;
quit;
PDevi
Fluorite | Level 6

@PGStatsIt worked, thank you.

PGStats
Opal | Level 21

Try it. Ask a new question if you get stuck.The Forum is there for you.

PG
PDevi
Fluorite | Level 6
I tested and it worked. 🙂 Thanks for your encouragement.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 2016 views
  • 3 likes
  • 3 in conversation