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:
Why is this happening?
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:
Why is this happening?
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:
Why is this happening?
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.
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;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.