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.

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