convert univariate table to multivariate table in sas [duplicate] - sql

This question already has answers here:
SAS PROC Transpose Data
(3 answers)
Closed 8 years ago.
I'm struggling with convert univariate table to multivariate table in sas. I would say the 'univariate table' I mean maybe still a multivariate table...but here's an example:
a b c
1001 1 4 8
1001 2 3 7
1002 11 9 6
1002 5 14 15
I want it to be like:
a1 b1 c1 a2 b2 c3
1001 1 4 8 2 3 7
1002 11 9 6 5 14 15
since I have thousands of ids(like 1001-3000). is there an simple way that I can flip the table around??
Many Thanks!

Not a simple way because you're doing multiple values.
You can do a transpose 3 times - once for each values and merge via the link from #jaamor. Or you can do a semi-manual data step transpose. This assumes a max count of 2. If you have more than 2 per ID you can calculate the max and put it into a macro variable. Then replace the 2's in the code with the macro variable.
data want;
set have;
by id;
array _a(2) a1-a2;
array _b(2) b1-b2;
array _c(2) c1-c2;
retain a: b: c:;
if first.id then count=1;
else count+1;
_a(count)=a;
_b(count)=b;
_c(count)=c;
if last.id then output;
drop a b c count;
run;

Related

Calculate new column using relative row references

I would like to turn a data frame like this:
DF
Nrow
a
1
5
2
6
3
7
4
11
5
16
Into this:
DF
Nrow
a
b
1
5
NA
2
6
NA
3
7
2
4
11
5
5
16
9
Column 'b' is calculated as the value from column 'a' minus another value from column 'a', in [row-2]. For example b4 = a4-a2.
I have had no success so far with indexing or loops. Is there a tool or command for this or some obvious notation that I am missing? I need to do this continuously without splitting into groups.

Remove a string from certain column values and then operate them Pandas

I have a dataframe with a column named months (as bellow), but it contains some vales passed as "x years". So I want to remove the word "years" and multiplicate them for 12 so all column is consistent.
index months
1 5
2 7
3 3 years
3 9
4 10 years
I tried with
if df['months'].str.contains("years")==True:
df['df'].str.rstrip('years').astype(float) * 12
But it's not working
You can create a multiplier series based on index with "years" and multiply those months by 12
multiplier = np.where(df['months'].str.contains('years'), 12,1)
df['months'] = df['months'].str.replace('years','').astype(int)*multiplier
You get
index months
0 1 5
1 2 7
2 3 36
3 3 9
4 4 120
Slice and then use replace()
indexs = df['months'].str.contains("years")
df.loc[indexs , 'months'] = df['a'].str.replace("years" , "").astype(float) * 12

Dataframe group by numerical column and then combine with the original dataframe [duplicate]

This question already has answers here:
Pandas new column from groupby averages
(2 answers)
Closed 2 years ago.
I have a pandas data frame and I would like to first group by one of the columns and calculate mean of count of each group of that column. Then, I would like to combine this grouped entity with the original data frame.
An example:
df =
a b orders
1 3 5
5 8 10
2 3 6
Group by along column b and taking mean of orders
groupby_df =
b mean(orders)
3 5.5
8 10
End result:
df =
a b orders. mean(orders)
1 3 5 5.5
5 8 10 10
2 3 6 5.5
I know I can group by on b and then, do a inner join on b, but, I feel like it can be done in much cleaner/one-liner way. Is it possible to do better than that?
This is transform
df['mean']=df.groupby('b').orders.transform('mean')

Remove all rows which each value is the same [duplicate]

This question already has answers here:
How do I get a list of all the duplicate items using pandas in python?
(13 answers)
Closed 3 years ago.
I want to drop all rows that have same values by drop_duplicates(subset=['other_things','Dist_1','Dist_2']) but could not get it.
Input
id other_things Dist_1 Dist_2
1 a a a
2 a b a
3 10 10 10
4 a b a
5 8 12 48
6 8 12 48
Expeted
id other_things Dist_1 Dist_2
2 a b a
4 a b a
5 8 12 48
6 8 12 48
Try
df = df.drop_duplicates()
It looks like the 'id' column could be generating problems.
Would recommend using the 'subset' parameter on drop duplicates as per the documentation.
drop_duplicates documentation1

Create new ID based on cumulative sum in excel vba

I need to create a new transport ID based on the cumulative sum of the volume being transported. Let´s say that originally everything was transported in truck A with a capacity of 25. Now I want to assign these items to shipments with truck B (Capacity 15).
The only real constraint is amt shipped cannot exceed capacity.
I can´t post a picture because of the restrictions...but the overall set up would be like this:
Old Trans # Volume New Trans # Cumulative Volume for Trans
1 1
1 9
1 3
1 7
1 4
2 9
2 10
3 8
3 5
3 9
4 4
4 6
4 8
5 9
5 1
5 5
5 8
6 3
6 4
6 3
6 4
6 4
6 7
7 7
7 10
7 4
8 10
8 6
8 7
9 4
9 9
9 6
10 7
10 4
10 1
10 1
10 5
10 2
11 9
11 3
11 9
12 8
12 5
12 9
13 9
Expected output would be that the first three entries would result in a new shipment ID of 1;the next two entries would result in a new shipment ID of 2;and so on... I´ve tried everthing that I know(excluding VBA): Index/lookup/if functions. My VBA skills are very limited though.Any tips?? thanks!
I think I see what you're trying to do here, and just using an IF formula (and inserting a new column to keep track):
In the Columns C and D, insert these formulas in row 3 and copy down (changing 15 for whatever you want your new volume capacity to be):
Column C: =IF(B3+C2<15,B3+C2,B3)
Column D: =IF(B3+C2<15,D2,D2+1)
And for the cells C2 and D2:
C2: = B2
D2: = A2
Is this what you're looking to do?
A simple formula could be written that 'floats' the range totals for each successive load ID.
In the following, I've typed 25 and 15 in D1:E1 and used a custom number format of I\D 0. In this way, the column is identified and the cell can be referenced as a true number load limit. You can hard-code the limits into the formula if you prefer by overwriting D$1 but you will not have a one-size-fits-all formula that can be copied right for alternate load limits as I have in my example..
      
The formula in D2 is,
=IF(ROW()=2, 1, (SUM(INDEX($B:$B, MATCH(D1, D1:D$1, 0)):$B2)>D$1)+ D1)
Fill right to E2 then down as necessary.