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

Hi.  I have a data set that contains some variables that I need to identify if it's the first and only occurrence, or first of multiple, and create new fields in a new data set that can be reviewed further.  The data sets have thousands of lines.  

 

Here's an example of what I am starting with:

id line exclude_ind reimb_amt
12345 01 00 20
12345 02 00 0
54321 01 01 10
54321 02 01 10
54321 03 00 5
34567 01 05 0
34567 02 05 15
34567 03 09 20
34567 04 03 10

From the above data set, I want a field named id_count, which identifies unique id's.  Each unique id gets a value = 01.  

I want to create a field called exclude_ind_count, which is made of a value = 01 for each unique value in the exclude_ind field, within the unique id field.  

 

So, the resulting new data set looks like this:

id id_count line exclude_ind exclude_ind_count reimb_amt
12345 01 01 00 01 20
12345 00 02 00 00 0
54321 01 01 01 01 10
54321 00 02 01 00 10
54321 00 03 00 01 5
34567 01 01 05 01 0
34567 00 02 05 00 15
34567 00 03 09 01 20
34567 00 04 03 01 10

So, each id_count field should, when summed, = 1 for each unique id.  Basically I want a count of how many unique id's are in my data set.  

And, as shown, within each unique id, each unique instance of exclude_ind has a resulting exclude_ind_count = 01.  So, when summed, I can get an idea of how many id's have either 1 exclude_ind, or multiple exclude_ind.  

1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19

I think this is what you want, or close.   You may need to sort by id exclude_ind.

 

data have;
   infile cards dlm='09'x dsd;
   input id line exclude_ind reimb_amt;
   cards;
12345	01	00	20
12345	02	00	0
54321	01	01	10
54321	02	01	10
54321	03	00	5
34567	01	05	0
34567	02	05	15
34567	03	09	20
34567	04	03	10
;;;;
   run;
   
data want;
   set have;
   by id exclude_ind notsorted;
   id_count = first.id;
   exclude_ind_count = first.exclude_ind;
   run;   

Capture.PNG

View solution in original post

3 REPLIES 3
ballardw
Super User

Is that id_count supposed to be numeric or character? Not at all clear as showing two characters makes it a bit odd.

 

Is your data sorted by  ID?

 

If your data is at least grouped by ID (all the ID values in adjacent rows) AND the Exclude_ind are also grouped together then creating NUMERIC values for this is easy. If not grouped then you may need to provide an example of that data and what you expect, which might require sorting the data.

 

data have;
  input id 	line 	exclude_ind 	reimb_amt;
datalines;
12345 	01 	00 	20
12345 	02 	00 	0
54321 	01 	01 	10
54321 	02 	01 	10
54321 	03 	00 	5
34567 	01 	05 	0
34567 	02 	05 	15
34567 	03 	09 	20
34567 	04 	03 	10
;

data want;
   set have;
   by id exclude_ind notsorted;
   id_count=first.id;
   exclude_ind_count=first.exclude_ind;
run;

If you must see two digits for those variables then assign a Z2. format. Of you want a character version then use

put(first.var,z2.)

 

When you use BY group processing in a data step SAS provides automatic numeric 1/0 valued (1=true 0=false) variables FIRST. and LAST. for each variable on the By statement indicating whether the current observation is the first or last of that group.

 

 

JH74
Obsidian | Level 7
Thank you. I appreciate your help. Pretty much the same answer as the other user added. It worked.
data_null__
Jade | Level 19

I think this is what you want, or close.   You may need to sort by id exclude_ind.

 

data have;
   infile cards dlm='09'x dsd;
   input id line exclude_ind reimb_amt;
   cards;
12345	01	00	20
12345	02	00	0
54321	01	01	10
54321	02	01	10
54321	03	00	5
34567	01	05	0
34567	02	05	15
34567	03	09	20
34567	04	03	10
;;;;
   run;
   
data want;
   set have;
   by id exclude_ind notsorted;
   id_count = first.id;
   exclude_ind_count = first.exclude_ind;
   run;   

Capture.PNG

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 3 replies
  • 292 views
  • 2 likes
  • 3 in conversation