DATA Step, Macro, Functions and more

Create a new variable

Accepted Solution Solved
Reply
Contributor
Posts: 22
Accepted Solution

Create a new variable

I am trying to create a new variable that has a value of 'A' for the first 30 observations (from 1 to 30), and a value of 'B' for the remaining 30 observations (from 31 to 60). Here it is my code and the data set: 

 

DATA draft;
INFILE "/folders/myfolders/2017NBADraft2.txt"
DLM=',' FIRSTOBS=2 DSD MISSOVER;
INPUT LastName $ FirstName $ Team $ Position $ Birthdate :ANYDTDTE10. Height Wingspan Weight College $ Year $;
RUN;

PROC PRINT DATA=draft;
RUN;

DATA draft;
SET draft;
IF Obs < 31 then New = 'A';
ELSE IF Obs > 30 then New = 'B';
RUN;

PROC PRINT DATA=draft;
RUN;

 This is assigning A to all the variables and creating a new variable 'Obs'... how can I optimize this code in order to generate the desired output?


Accepted Solutions
Solution
‎04-21-2018 02:41 PM
Trusted Advisor
Posts: 1,828

Re: Create a new variable

[ Edited ]

There is no variable OBS in your data therefor it is assigned as missing value which is the lowest posibble value

and obviousley less then 31.

 

Change OBS by _N_, which is an internal counter, and you'll get what you want.

View solution in original post


All Replies
Solution
‎04-21-2018 02:41 PM
Trusted Advisor
Posts: 1,828

Re: Create a new variable

[ Edited ]

There is no variable OBS in your data therefor it is assigned as missing value which is the lowest posibble value

and obviousley less then 31.

 

Change OBS by _N_, which is an internal counter, and you'll get what you want.

PROC Star
Posts: 8,117

Re: Create a new variable

I agree with @Shmuel, but you don't need the extra if. i.e.:

DATA draft;
  SET draft;
  IF  _n_ < 31 then  New = 'A';
  ELSE New = 'B';
RUN;

Art, CEO, AnalystFinder.com

 

☑ This topic is solved.

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

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