BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
sasphd
Lapis Lazuli | Level 10

Hello, 

I want de create a variable tag1 which takes a value of 1 if by year et ticker

TABLE HAVE

ticker  FYEAR

A           2015

A           2016

A           2016

B          2015

B          2016

B           2017

B            2017

 

TABLE WANT

ticker  FYEAR     TAG1

A           2015        1

A           2016        1

A           2016        0

B          2015        1

B          2016        1

B           2017       1

B            2017      0

 

 

thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Perhaps:

data HAVE;
input ticker $  FYEAR ;
datalines;
A           2015
A           2016
A           2016
B          2015
B          2016
B           2017
B            2017
;

data want;
   set have;
   by ticker fyear;
   tag1= first.fyear;
run;

The above assumes the data is sorted by ticker and fyear. If the actual is not sorted but is grouped then adding the NOTSORTED option to the BY statement in the Data Want step may work. If not, then sort the data first is likely the approach.

 

When you use a BY statement SAS creates automatic variables First.Variablename and Last.Variable name that indicate whether an observation is the first or last of the by group for each variable. These values are 1, for true, and 0, for false.

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

So any record is a duplicate year for a given ticker gets a TAG1 value of 0? Or are the rules more complicated than that? From such a small example, it's hard to deduce rules. Please tell us.

--
Paige Miller
ballardw
Super User

Perhaps:

data HAVE;
input ticker $  FYEAR ;
datalines;
A           2015
A           2016
A           2016
B          2015
B          2016
B           2017
B            2017
;

data want;
   set have;
   by ticker fyear;
   tag1= first.fyear;
run;

The above assumes the data is sorted by ticker and fyear. If the actual is not sorted but is grouped then adding the NOTSORTED option to the BY statement in the Data Want step may work. If not, then sort the data first is likely the approach.

 

When you use a BY statement SAS creates automatic variables First.Variablename and Last.Variable name that indicate whether an observation is the first or last of the by group for each variable. These values are 1, for true, and 0, for false.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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