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

 

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!

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.

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