BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
NewUsrStat
Pyrite | Level 9

Hi guys, 

suppose to have the following dataset: 

 

data DB;
  input ID :$20. Admission :date09. Discharge :date09. Code1 Code2 Code3;
  format Admission date9. Discharge date9.;
cards;
0001  13JAN2017 25JAN2017  345   6    8
0001  22FEB2018 03MAR2018   5   21   64
0001  30JAN2019 04MAR2019  V45   6   43
0002  01DEC2016 14DEC2016  56    0   998
0002  01DEC2016 14DEC2016  54   V321 65
0002  25DEC2017 02JAN2018  543  987  098
0002  06JAN2018 09JAN2018  21   468  765
;run;

and a list of codes in combination as follows: 

1) 345, 8

2) 543, 098

 

Is there a way to count how many times the combinations occur in DB (looking at variables Code1, Code2, Code3), and subset the records (rows) where they appear and save into two different datasets according to the combinations? In other words two sub-datasets will be generated: one for combination 1) and the other for combination 2).

 

Thank you in advance

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

I would suggest keeping the data in one dataset.

Assuming you want to check for both codes on the same observation then you could code it this way.

data DB;
  length ID $20 Admission Discharge 8  Code1-Code3 $5 ;
  input id -- code3;
  format Admission Discharge date9.;
  informat Admission Discharge date.;
cards;
0001  13JAN2017 25JAN2017  345   6    8
0001  22FEB2018 03MAR2018   5   21   64
0001  30JAN2019 04MAR2019  V45   6   43
0002  01DEC2016 14DEC2016  56    0   998
0002  01DEC2016 14DEC2016  54   V321 65
0002  25DEC2017 02JAN2018  543  987  098
0002  06JAN2018 09JAN2018  21   468  765
;

data want;
  set db ;
  array dx code1-code3;
  flag1= ('345' in dx) and ('8' in dx);
  flag2= ('543' in dx) and ('098' in dx);
run;

Result

Tom_0-1727358109187.png

 

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

Partial code:

 

data want1 want2;
    set db;
    if whichn(345,of code1-code3)>0 and whichn(8,of code1-code3)>0 then output want1;

 

See if you can program the rest from the above fragment. (Question: it seems as if code1-code3 is really character variables, but you have programmed them as numeric, is that right?)

 

Advice: create flag variables FLAG1 and FLAG2 and leave everything in one data set. Use this one data set instead of two separate data sets. If you have FLAG1 and FLAG2, then PROC FREQ computes the counts easily.

--
Paige Miller
Tom
Super User Tom
Super User

I would suggest keeping the data in one dataset.

Assuming you want to check for both codes on the same observation then you could code it this way.

data DB;
  length ID $20 Admission Discharge 8  Code1-Code3 $5 ;
  input id -- code3;
  format Admission Discharge date9.;
  informat Admission Discharge date.;
cards;
0001  13JAN2017 25JAN2017  345   6    8
0001  22FEB2018 03MAR2018   5   21   64
0001  30JAN2019 04MAR2019  V45   6   43
0002  01DEC2016 14DEC2016  56    0   998
0002  01DEC2016 14DEC2016  54   V321 65
0002  25DEC2017 02JAN2018  543  987  098
0002  06JAN2018 09JAN2018  21   468  765
;

data want;
  set db ;
  array dx code1-code3;
  flag1= ('345' in dx) and ('8' in dx);
  flag2= ('543' in dx) and ('098' in dx);
run;

Result

Tom_0-1727358109187.png

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 121 views
  • 3 likes
  • 3 in conversation