This is what I have:
This is what I want to get:
This is my code, which does not work:
proc transpose data = ratings_long out = ratings_wide prefix=Item;
by ComID;
id Item;
var Value;
run;
You need to include FisYear if you want it to be part of the process.
proc transpose data = ratings_long out = ratings_wide prefix=Item;
by ComID FisYear;
id Item;
var Value;
run;
Your display is not sorted so you might need to first call PROC SORT to sort the data by COMID FISYEAR.
Related
I have the following data strucure, which I need to transform:
I have been working on transposing the data properly to obtain a table structure.
Target structure
Attribute 1, Attribute 2, ...., Attribute n
Outcome1-A, Outcome2-A, ...., Outcome n-A .....
....
Outcome A-Z, Outcome2-Z, ...., Outcome n-Z .....
I started with the following statement, modifications would be great. The static names of the attributes are imported as duplicate records.
PROC TRANSPOSE DATA=INPUT_TAB OUT=vertikal ;
VAR v name ;
id n;
RUN;
The id statement serves tels SAS which variable to use for column names, and if I understand your question well, that is Name, not n. Hence Name should not be in the VAR list
You should also tell SAS when to start a new observation (aka row), using a by statement. Probably one of the columns left of Name is a good candidate. Let's name it group_id. Hence the code should look like this:
PROC TRANSPOSE DATA=INPUT_TAB OUT=vertikal ;
by group_id;
VAR v;
id Name;
RUN;
If there is no variable like group_id available, you can create one this way:
DATA INPUT_VIEW / view=INPUT_VIEW;
set INPUT_TAB;
retain group_id 1;
if Name eq "SZENARIO_ID" then group_id = group_id + 1;
PROC TRANSPOSE DATA=INPUT_VIEW OUT=vertikal ;
by group_id;
VAR v;
id Name;
RUN;
I choose a view to avoid needlessly writing data to disk, but that only matters for large datasets.
I expanded the table using COL_TYP, however the transpose command does not work, see error message
PROC SQL;
CREATE TABLE TEST_CASE_WHEN AS
SELECT A.*,
CASE WHEN A.NAME IN ('SZENARIO_ID') THEN 1
ELSE 0 END AS COL_TYP
FROM WORK.TEST A
;
QUIT;
proc sort
data = work.INPUT_VIEW out= input_view2;
BY N;
RUN;
PROC TRANSPOSE DATA=INPUT_VIEW2 OUT=vertikal ;
by COL_TYP;
VAR v;
id Name;
RUN;
ERROR: Data set WORK.INPUT_VIEW2 is not sorted in ascending sequence. The current BY group has
COL_TYP = 2 and the next BY group has COL_TYP = 0.
Thanks and Kind Regards
Kingsley
So I have a dataset that has the sample size and the seed for each stratum.
How do I reference these in a proc surveyselect?
My Code:
proc surveyselect data=hca2 (where =(disp=1 and fin=1 and dol_str=1))
out=Disp1_Fin1_DS1
method=SRS
seed=seed
sampsize=samp_n;
run;
Does anyone know how to do this?
There is a secondary input data set reference in proc surveyselect, which may be your anwser.
SAS Help Center: Secondary Input Data Set
And the following is a simple example:
proc sort data = sashelp.class out = class;
by sex name;
run;
data config;
do sex = 'F','M';
_seed_ = 42;
_nsize_ = 6;
output;
end;
run;
proc surveyselect data = class out = result method = srs seed=config sampsize=config outseed;
strata sex;
run;
Open data set result and see if it is what you need.
I have the original dataset
What I want is:
My code:
proc transpose data = lib.original
out= lib.new(rename=(col1=Mean col2=Median));
var WBCmean RBCmean WBCmedian RBCmedian;
run;
But I get
Can you give some hint?
EDIT
If I add by statement,
proc transpose data = lib.original
out= lib.new;
by Gender;
var WBCmean RBCmean WBCmedian RBCmedian;
run;
then I get
One way is to convert to a tall skinny format.
proc transpose data = have out=middle ;
by gender ;
var WBCmean RBCmean WBCmedian RBCmedian;
run;
Then generate the new variables needed,
data middle;
set middle ;
testcode = substr(_name_,1,3);
_name_ = substr(_name_,4);
run;
you might need to sort the data depending on the order of variable names in your original VAR statement,
proc sort data=middle;
by gender testcode _name_;
run;
and then transpose into the new arrangement.
proc transpose data=middle out=want ;
by gender testcode ;
id _name_;
var col1 ;
run;
Here's what I'm doing but it throws an error saying. It will print the first two but fails after that.
ERROR: Data set WORK.FISH is not sorted in ascending sequence. The current BY group has Species = Whitefish and the next BY group
has Species = Parkki.
Any idea what I'm doing wrong?
data fish;
set sashelp.fish;
run;
proc means data = fish;
var weight;
by species;
run;
The BY statement in proc means assumes that the dataset is sorted by the BY variable. Just add a proc sort before proc means.
data fish;
set sashelp.fish;
run;
proc sort data=fish;
by species;
run;
proc means data = fish;
var weight;
by species;
run;
Another way of doing it without the proc sort is to use a class statement:
proc means data=sashelp.fish mean;
class species;
var weight;
run;
I need help with proc transpose procedure in SAS. My code initially was:
proc transpose data=temp out=temp1;
by patid;
var text;
Id datanumber;
run;
This gave me error "The ID value " " occurs twice in the same BY group". I modified the code to this:
proc sort data = temp;
by patid text datanumber;
run;
data temp;
set temp by patid text datanumber;
if first.datanunmber then n = 0;
n+1;
run;
proc sort data = temp;
by patid text datanumber n;
run;
proc transpose out=temp1 (drop=n) let;
by patid;
var text;
id datanumber;
run;
This is giving me error: variable n is not recognized. Adding a let option is giving a lot of error "occurs twice in the same BY group". I want to keep all id values.
Please help me in this.
Data Example:
Patid Text
When you get that error it is telling you that you have multiple data points for one or more variables that you are trying to create. SAS can force the transpose and delete the extra datapoints if you add "let" to the proc transpose line.
Your data is possibly not unique? I created a dataset (with unique values of patid and datanumber) and the transpose works:
data temp (drop=x y);
do x=1 to 4;
PATID='PATID'||left(x);
do y=1 to 3;
DATANUMBER='DATA'||left(y);
TEXT='TEXT'||left(x*y);
output;
end;
end;
proc sort; by _all_;
proc transpose out=temp2 (drop=_name_);
by patid;
var text;
id datanumber;
run;
my recommendation would be to forget the 'n' fix and focus on making the data unique for patid and datanumber, a dirty approach would be:
proc sort data = temp nodupkey;
by patid datanumber;
run;
at the start of your code..
Try to sort your dataset by patid text n datanumber, (n before datanumber).
Try to sort your dataset by patid n datanumber, (n before datanumber). and proc transpose "by patib n ";