BookmarkSubscribeRSS Feed
foxrol94
Fluorite | Level 6

Hi,

 

I have a dataset ONE and want to :

  1. Duplicate ONE dataset
  2. Each row is now doubled. So for each pair I will want to create a new variable top which is 'x' on the first row and nothing on the second.

The code below only gives me the character 'x' on the second line when I would like it on the first

 

data TMP_ONE;
	set ONE;
	top = 'x';
	run;

data TMP_1_ONE;
	set ONE;
	top = '';
	run;
data I_ONE;
set TMP_ONE TMP_1_ONE;
run;

proc sort data=I_ONE out=I_TWO;
by _all_  top;
run;

Thanks

 

4 REPLIES 4
PaigeMiller
Diamond | Level 26

I think this does what you want, all in one DATA step.

 

data want;
    set ONE;
    top = 'x';
    output;
    top = ' ';
    output;
run;
--
Paige Miller
ballardw
Super User

@foxrol94 wrote:

Hi,

 

I have a dataset ONE and want to :

  1. Duplicate ONE dataset
  2. Each row is now doubled. So for each pair I will want to create a new variable top which is 'x' on the first row and nothing on the second.

The code below only gives me the character 'x' on the second line when I would like it on the first

 

data TMP_ONE;
	set ONE;
	top = 'x';
	run;

data TMP_1_ONE;
	set ONE;
	top = '';
	run;
data I_ONE;
set TMP_ONE TMP_1_ONE;
run;

proc sort data=I_ONE out=I_TWO;
by _all_  top;
run;

Thanks

 


Your sort says to sort by the order of TOP. Blank comes before 'X'. If you want the 'X' first, assuming no other value of Top is 'larger' or would come after X alphabetically then

proc sort data=I_ONE out=I_TWO;
   by _all_  descending top;
run;

 

PaigeMiller
Diamond | Level 26

@ballardw this doesn't work for me.

 

data TMP_ONE;
	set sashelp.class;
	top = 'x';
	run;

data TMP_1_ONE;
	set  sashelp.class;
	top = ' ';
	run;
data I_ONE;
set TMP_ONE TMP_1_ONE;
run;

proc sort data=I_ONE out=I_TWO;
   by _all_  descending top;
run;

does not produce the requested ordering because (I think) the BY statement in PROC SORT is now really

 

by name sex age height weight top descending top;

so first TOP is sorted ascending and then within that TOP is sorted descending, and the first sort of TOP in ascending order "overpowers" the second sort of TOP in descending order.

--
Paige Miller
ballardw
Super User

@PaigeMiller wrote:

@ballardw this doesn't work for me.

 

data TMP_ONE;
	set sashelp.class;
	top = 'x';
	run;

data TMP_1_ONE;
	set  sashelp.class;
	top = ' ';
	run;
data I_ONE;
set TMP_ONE TMP_1_ONE;
run;

proc sort data=I_ONE out=I_TWO;
   by _all_  descending top;
run;

does not produce the requested ordering because (I think) the BY statement in PROC SORT is now really

 

by name sex age height weight top descending top;

so first TOP is sorted ascending and then within that TOP is sorted descending, and the first sort of TOP in ascending order "overpowers" the second sort of TOP in descending order.


Right. I never use _all_ for sort so forgot the variable Top would in effect be on the By twice.

But perhaps the OP can actually list the variables that are needed for the sort by name and have the variable in correct position in the BY statements.

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