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

Hello,

I'm having difficulty figuring out why SAS is misidentifying variable responses. I'd like to create a new binary variable of 'W00' and 'NO' based on whether the variable response for NEWICD is 'W00' (i.e., if the response is 'W00' for NEWICD, then PROB1 will be 'W00' and if not PROB1 will be 'NO'). My code is:

DATA INJLIB.NT;
IF NEWICD ='W00' THEN PROB1='W00';
ELSE IF NEWICD NE 'W00' THEN PROB1 ='NO';
SET INJLIB.NT;
RUN;

However, these are the first ten lines of the output data:

 

Angmar_0-1613584757942.png

Why is this happening?

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Order of operations, put the SET statement BEFORE the logic code - you need data to operate on.

Note that you'll need to replace your NT data set first from the original source as you've likely messed it up. This is why that style of programming, using the same name in the DATA and SET statements is not recommended.


DATA INJLIB.NT2;
SET INJLIB.NT;
IF NEWICD ='W00' THEN PROB1='W00';
ELSE IF NEWICD NE 'W00' THEN PROB1 ='NO';

RUN;

@Angmar wrote:

Hello,

I'm having difficulty figuring out why SAS is misidentifying variable responses. I'd like to create a new binary variable of 'W00' and 'NO' based on whether the variable response for NEWICD is 'W00' (i.e., if the response is 'W00' for NEWICD, then PROB1 will be 'W00' and if not PROB1 will be 'NO'). My code is:

DATA INJLIB.NT;
IF NEWICD ='W00' THEN PROB1='W00';
ELSE IF NEWICD NE 'W00' THEN PROB1 ='NO';
SET INJLIB.NT;
RUN;

However, these are the first ten lines of the output data:

 

Angmar_0-1613584757942.png

Why is this happening?




View solution in original post

3 REPLIES 3
Reeza
Super User

Order of operations, put the SET statement BEFORE the logic code - you need data to operate on.

Note that you'll need to replace your NT data set first from the original source as you've likely messed it up. This is why that style of programming, using the same name in the DATA and SET statements is not recommended.


DATA INJLIB.NT2;
SET INJLIB.NT;
IF NEWICD ='W00' THEN PROB1='W00';
ELSE IF NEWICD NE 'W00' THEN PROB1 ='NO';

RUN;

@Angmar wrote:

Hello,

I'm having difficulty figuring out why SAS is misidentifying variable responses. I'd like to create a new binary variable of 'W00' and 'NO' based on whether the variable response for NEWICD is 'W00' (i.e., if the response is 'W00' for NEWICD, then PROB1 will be 'W00' and if not PROB1 will be 'NO'). My code is:

DATA INJLIB.NT;
IF NEWICD ='W00' THEN PROB1='W00';
ELSE IF NEWICD NE 'W00' THEN PROB1 ='NO';
SET INJLIB.NT;
RUN;

However, these are the first ten lines of the output data:

 

Angmar_0-1613584757942.png

Why is this happening?




PaigeMiller
Diamond | Level 26

The only obvious problem is the location of the SET statement. It should be:

 

DATA INJLIB.NT;
SET INJLIB.NT;
IF NEWICD ='W00' THEN PROB1='W00';
ELSE IF NEWICD NE 'W00' THEN PROB1 ='NO';
RUN;

If that's not it, then please provide a portion of your original data following these instructions, and not via any other method.

 

Also, code which overwrites the original data set by creating a new data set with the exact same name as the original data set isn't a good idea, and in rare cases could cause problems like you are seeing.

--
Paige Miller
Tom
Super User Tom
Super User

You have your statements out of order.  The SET statement is an executable statement, not a declarative statement.

 

You are calculating PROB1 before reading in the current value of NEWCID so the calculations are being done on the previous value of NEWCID.

DATA INJLIB.NT;
  SET INJLIB.NT;
  IF NEWICD ='W00' THEN PROB1='W00';
  ELSE IF NEWICD NE 'W00' THEN PROB1 ='NO';
RUN;

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
  • 3 replies
  • 874 views
  • 0 likes
  • 4 in conversation