DATA Step, Macro, Functions and more

Enumeration Variable Not working

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

Enumeration Variable Not working

Hi,

 

I'm trying to get my enumeration variable to work but I'm getting funky results. I need to set up a counter that will start at one for a specific item type and then add one for each additional ID. For example this is what I would want:

 

IDItem TypeCount
219202Srcrew Driver1
200138Srcrew Driver1
200143Srcrew Driver1
200656Srcrew Driver1
202054Wrench1
202239Srcrew Driver1
202239Srcrew Driver2
202239Wrench3
202240Srcrew Driver1
202242Srcrew Driver1
202242Wrench2
202244Srcrew Driver1
202246Srcrew Driver1
203606Srcrew Driver1
204083Wrench1
204083Wrench2
204084Wrench1
204086Wrench1
204086Wrench2
204086Wrench3

 

However, this is what I'm getting:

 

IDItem TypeCount
219202Srcrew Driver1
200138Srcrew Driver1
200143Srcrew Driver1
200656Srcrew Driver1
202054Wrench1
202239Srcrew Driver1
202239Srcrew Driver2
202239Wrench2296
202240Srcrew Driver1
202242Srcrew Driver1
202242Wrench2297
202244Srcrew Driver1
202246Srcrew Driver1
203606Srcrew Driver1
204083Wrench1
204083Wrench3
204084Wrench1
204086Wrench1
204086Wrench3
204086Wrench4

 

Here's my code:

 

proc sort data=need_id;

            by id type;

run;

 

data need_id2;

            set need_id;

            by id;

            count+1;

            if first.id then count=1;

run;

 

My thought is that if I sort the data set in the way that I want, with specific items being counted first, that the proper count will be assigned. However, that's not the case and I don't see a pattern as to why the counter isn't working.

 

Any thoughts or suggestions would be extreemly helpful!

 

Thanks in advance!


Accepted Solutions
Solution
‎09-29-2017 10:13 AM
Super User
Posts: 11,343

Re: Enumeration Variable Not working

Since you have data like this

202242 Srcrew Driver 1
202242 Wrench 2297

where the ID is duplicated did mean to count Id Type combinations, Id's or Types?

It sounds like you may have meant:

data need_id2;
            set need_id;
            by id type;
            count+1;
            if first.type then count=1;

run;

given your sort order.

 

 

You may also have some issues with your input data but since you did not provide that I'm not sure.

Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

And someone needs to learn how to specll screw driver

View solution in original post


All Replies
PROC Star
Posts: 763

Re: Enumeration Variable Not working

data have;
input ID$ Item_Type$20.;
infile datalines dlm=',' dsd;
datalines;
219202,Srcrew Driver
200138,Srcrew Driver
200143,Srcrew Driver
200656,Srcrew Driver
202054,Wrench
202239,Srcrew Driver
202239,Srcrew Driver
202239,Wrench
202240,Srcrew Driver
202242,Srcrew Driver
202242,Wrench
202244,Srcrew Driver
202246,Srcrew Driver
203606,Srcrew Driver
204083,Wrench
204083,Wrench
204084,Wrench
204086,Wrench
204086,Wrench
204086,Wrench
;

proc sort data=have;
   by id;
run;

data want;
   set have;
   by id;
   counter + 1;
   if first.id then counter=1;
run;
Solution
‎09-29-2017 10:13 AM
Super User
Posts: 11,343

Re: Enumeration Variable Not working

Since you have data like this

202242 Srcrew Driver 1
202242 Wrench 2297

where the ID is duplicated did mean to count Id Type combinations, Id's or Types?

It sounds like you may have meant:

data need_id2;
            set need_id;
            by id type;
            count+1;
            if first.type then count=1;

run;

given your sort order.

 

 

You may also have some issues with your input data but since you did not provide that I'm not sure.

Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

And someone needs to learn how to specll screw driver

Trusted Advisor
Posts: 1,022

Re: Enumeration Variable Not working

I see nothing wrong with your code.

 

However ... is variable COUNT already in your data set NEED_ID?  That could produce the weird results.

 

If so, then change

    SET NEED_ID ;

to

    SET NEED_ID (drop=count);

 

 

 

 

    PRFOC SORT

Occasional Contributor
Posts: 5

Re: Enumeration Variable Not working

Hi,

 

Man, I feel like a goof! Thanks to your suggestions, I realized that there was already a count variable in my dataset, which was causing the error. I dropped the historic count variable and then my code worked! No more coding late on a Friday afternoon after a long week!

 

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 158 views
  • 0 likes
  • 4 in conversation