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

Hello,

I have a data table that has rows that I want to give a number too based on a variable.  For example

I have the following data:

col1

------------

.

20

.

.

6

6

6

20

20

20

20

20

I want to create another variable called contentid.  The data should be like this

col1   contentid

------------

.       1

20     1

.       1

.       1

6      2

6      2

6      2

.      2

20    3

.      3

20  3

20  3

20  3

20  3

I've used a data step with a retain statement but i'm not sure at what point to increment  the value of contentid.  The fact there is missing rows is causing me the hardest time.

This is what I have so far

data test ;

  length contentid $2 ;

    set x ;       

  retain contentid ;

  if _n_ = 1 then contentid = 1;   

run ;

Thank you for any help.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Actually its the fact that the the first one is a value that makes my logic unclear.  How do you know the first one and 20 are the same?  Where does the extra missing come from the last series of 20's?

This seems a bit contrived, and not sure it will actually handle your data.

data want;

set have;

retain contentid 1 last;

if col1 ne . then do;

    if col1 ne last and last ne . then do;

    contentid+1;

    last=col1;

    end;

    else if col1 ne last then do;

    last=col1;

    end;

end;

run;

View solution in original post

2 REPLIES 2
Reeza
Super User

Actually its the fact that the the first one is a value that makes my logic unclear.  How do you know the first one and 20 are the same?  Where does the extra missing come from the last series of 20's?

This seems a bit contrived, and not sure it will actually handle your data.

data want;

set have;

retain contentid 1 last;

if col1 ne . then do;

    if col1 ne last and last ne . then do;

    contentid+1;

    last=col1;

    end;

    else if col1 ne last then do;

    last=col1;

    end;

end;

run;

jerry898969
Pyrite | Level 9

Hi Reeza,

Thank you for the reply. 

This is data coming from a data set someone else sends me.   It's the back end data file for a front end that shows data based on the content id.  The missing will generate a space when the user views the application.  That data was just an example of how the data could come to me and I have to tag the rows with the correct content id. 

That code worked for the example data I'm using.  I will run it against the full data set when it is sent to me.

Thank you so much.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 2 replies
  • 624 views
  • 0 likes
  • 2 in conversation