BookmarkSubscribeRSS Feed
nidhi123
Calcite | Level 5
Hello.

I am supposed to find out if this is possible:

DATA FILE1 FILE2;
SET FILE_IN;

IF X=Y THEN DO;
NEWVAR1=VAR1;
OUTPUT FILE1;
END;

ELSE DO;
NEWVAR2=VAR1;
OUTPUT FILE2;
END;

RUN;


Is it somehow possible that SAS with the help of some keyword only have newvar1 in file1 in addition to all other variables. And in file2 its only consist all variables + newvar2.

This is very simple with drop and keep. But is it possible without, make sas execute partly? May be this is to make it more difficult. But I just want to know if its possible in other way or not. I cannot figure out another way than using drop and keep.

Thanks
4 REPLIES 4
Robert_Bardos
Fluorite | Level 6
See the documentation for the RENAME dataset option.
Doc_Duke
Rhodochrosite | Level 12
You could also do it with the KEEP= or DROP= option on the DATA statement.
Sandhya
Fluorite | Level 6
Hi,

data file1 file2;
set file_in;
if x=y then output file1;
else output file2;
run;

proc datasets library=work;
modify file1;
rename var1=newvar1;
quit;
run;

proc dataset library=work;
modify file2;
rename var1=newvar2;
quit;
run;

Rename will give unwanted result if it is used within IF condition. Rename is a global statement. It creates the variable irrespective of the conditons checked.
Hence a seperate program for renaming.

Sandhya.
chang_y_chung_hotmail_com
Obsidian | Level 7
It is a good and efficient practice to use proc datasets to rename variables, since this will just modify the header portion of the dataset.

Sometimes, however, the data set options can be quite handy as well.

[pre]
/* test data */
data one;
x=1; y=1; var1=1; output;
x=2; y=3; var1=2; output;
x=3; y=2; var1=3; output;
run;

/* separate and rename */
data two(rename=(var1=newvar1))
three(rename=(var1=newvar2));
set one;
if x=y then output two;
else output three;
run;

/* check */
proc print data=two;
run;
proc print data=three;
run;
/* on lst
Obs x y newvar1

1 1 1 1

Obs x y newvar2
1 2 3 2
2 3 2 3
*/
[/pre]

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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