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

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

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

2 REPLIES 2
Shmuel
Garnet | Level 18

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.

art297
Opal | Level 21

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

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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