BookmarkSubscribeRSS Feed
butterfly_wing
Calcite | Level 5

Hello,

 

I am using SAS Studio and I tried to create a new variable using conditional logic. The new variable was created successfully (called NewVar).

 

My original dataset has 140 columns so I was expecting my data set to have 141 columns (with one new variable). However, my new data set only has 33 columns. Why is that?

 

Below are my codes, really appreciate your help! By the way, is there a way to write a statement to keep ALL the variables?

 

Data Data_2;
Set WORK.PRINTSORTEDDATA;
Length NewVar 4;
IF B6 = 1 THEN NewVar = 6;
ELSE IF B7 = 1 then NewVar = 5;
else if B7 = 2 then NewVar = 4;
else if B7 = 3 then NewVar = 3;
else if B7 = 4 then NewVar = 2;
else if B7 = 5 then NewVar = 1;
RUN;

 

2 REPLIES 2
kiranv_
Rhodochrosite | Level 12

Nothing in your code will decrease columns. use proc contents to see columns of both the tables, that will give you good idea on what is happening. 

Proc contents data = Data_2;
run;

Proc contents data = PRINTSORTEDDATA;
run;

/* You can use dictionay.columns to find what are columns which are not there. example code below and you can use the same for your code*/

data class;
		set sashelp.class(drop=age);
		run;

proc sql ;
 select coalesce(a.name, b.name) as missing_column
 from 
	   (select name from Dictionary.columns
	     where upcase(libname)=upcase('work') and 
	     upcase(memname)=upcase('class'))a 
full join
      (Select name from Dictionary.columns
       where upcase(libname)=upcase('SASHELP') and 
		upcase(memname)=upcase('CLASS'))b 
		
on upcase(a.name)=upcase(b.name) 
 where a.name is missing or b.name is missing;
Reeza
Super User

Check your log? You likely have an error somewhere and are working with a partial data set. 

 


@butterfly_wing wrote:

Hello,

 

I am using SAS Studio and I tried to create a new variable using conditional logic. The new variable was created successfully (called NewVar).

 

My original dataset has 140 columns so I was expecting my data set to have 141 columns (with one new variable). However, my new data set only has 33 columns. Why is that?

 

Below are my codes, really appreciate your help! By the way, is there a way to write a statement to keep ALL the variables?

 

Data Data_2;
Set WORK.PRINTSORTEDDATA;
Length NewVar 4;
IF B6 = 1 THEN NewVar = 6;
ELSE IF B7 = 1 then NewVar = 5;
else if B7 = 2 then NewVar = 4;
else if B7 = 3 then NewVar = 3;
else if B7 = 4 then NewVar = 2;
else if B7 = 5 then NewVar = 1;
RUN;

 


 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 1434 views
  • 4 likes
  • 3 in conversation