Hi,
I have a dataset ONE and want to :
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
I think this does what you want, all in one DATA step.
data want;
set ONE;
top = 'x';
output;
top = ' ';
output;
run;
@foxrol94 wrote:
Hi,
I have a dataset ONE and want to :
- Duplicate ONE dataset
- 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;
@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.
@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.
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.